2D-Array, gleiche Einträge prüfen

billbill

Mitglied
hi leute,
also ich hab ein 6x6 (0 bis 5 mal 0 bis 5) 2D array welches mann mit 2 verschieden werten von typ Char füllen kann
wenn 5 einträge horizonzal, vertikal oder diagonal gleich sind dann soll das ergebniss in "winner" übernommen werden
aber das klappt irgendwie nicht 🙁

Java:
	char winner;

		public char winner(){

		      for (int j = 0; j < 5; j++) {
			     if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && (board[2][j] == board[3][j]) && (board[3][j] == board[4][j])){
				         
			         winner = board[0][j];
			         break;
			         }			     
		         if ((board[j][0] == board[j][1]) && (board[j][1] == board[j][2]) && (board[j][2] == board[j][3]) && (board[j][3] == board[j][4])){ 
		         
		             winner = board[j][0];
		             break;
		         }
		      }
		      
		      for (int j = 1; j < 6; j++) {
			     if ((board[1][j] == board[2][j]) && (board[2][j] == board[3][j]) && (board[3][j] == board[4][j]) && (board[4][j] == board[5][j])){
				         
			         winner = board[0][j];
			         break;
			         }			     
		         if ((board[j][1] == board[j][2]) && (board[j][2] == board[j][3]) && (board[j][3] == board[j][4]) && (board[j][4] == board[j][5])){ 
		         
		             winner = board[j][0];
		             break;
		         }
		      }
		     
		      if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) && (board[3][3] == board[4][4])){
		    		  winner = board[0][0];
		    		  }
		      if ((board[0][5] == board[1][4]) && (board[1][4] == board[2][3]) && (board[2][3] == board[3][2]) && (board[3][2] == board[4][1])){ 	     
		            winner = board[0][5];
		      		}
		      if ((board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) && (board[3][3] == board[4][4]) && (board[4][4] == board[5][5])){
	    		  winner = board[1][1];	      
		      }
		      if ((board[1][4] == board[2][3]) && (board[2][3] == board[3][2]) && (board[3][2] == board[4][1]) && (board[4][1] == board[5][0])){ 	     
	            winner = board[1][4];
		      }
		         return winner;
		      }

kann mir irgendwer weiterhelfen?

danke im voraus
 
(Kleiner Einschub: Wenn du da noch ein paar Zeilen dazugeschrieben hättest, so dass man es rauskopieren, direkt compilieren und direkt starten kann, und vielleicht so scheinbar unbedeutende Details erwähnt hättest, wie z.B. WAS nicht funktioniert, dann gäbe es hier jetzt vermutlich schon mehrere, hilfreiche Antworten)
 
Hab das ganze mal überarbeitet:

Java:
import java.io.*;
import java.util.Scanner;

public class PentagoModel
{

	private char[][] board = new char[6][6];
	private int zeile;
	private int spalte;
	private char spieler;
	private int feld;
	private char direction;
	char winner;

	public PentagoModel()
	{
		zeile = 0;
		spalte = 0;
		spieler = 'R';

		if (board.length == 0 || board.length != board[0].length || board.length % 3 != 0)
		{
			throw new RuntimeException("Ungültige Spielfeldgröße");
		}

		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j < board[i].length; j++)
			{
				board[i][j] = ' ';
			}
		}
		board[0][0]=' ';
		winner = ' ';

	}

	public void displayBoard()
	{
		StringBuilder boardOutput = new StringBuilder();
		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j < board[i].length; j++)
			{
				boardOutput.append(board[i][j]);
				if (j < (board[i].length - 1))
				{
					boardOutput.append("  |  ");
				}
			}
			boardOutput.append("\n------------------------------------\n");
		}
		boardOutput.append("\n");
		System.out.println(boardOutput.toString());
	}

	public void setPosition()
	{
		board[zeile][spalte] = spieler;
	}
	
	public void turnField()
	{
		int xStart = (feld % (board.length / 3))*3;
		int yStart = (feld / (board.length / 3))*3;
		
		char[][] values = new char[3][3];
		for (int i = 0; i < values.length; i++)
		{
			for (int j = 0; j < values[i].length; j++)
			{
				values[i][j]=board[i+xStart][j+yStart];
			}
		}
		
		if (direction == 'L')
		{
			for (int x = 0; x < 3; x++)
			{
				int newY = x;
				for (int y = 0; y < 3; y++)
				{
					int newX = (3-1)-y;
					board[xStart + newX][yStart + newY]=values[x][y];
				}
			}
		} else
		// if(direction != 'R')
		{
			for (int x = 0; x < 3; x++)
			{
				int newY = (3-1)-x;
				for (int y = 0; y < 3; y++)
				{
					int newX = y;
					board[xStart + newX][yStart + newY]=values[x][y];
				}
			}
		}
	}
	
	public void nextPlayer()
	{
		if (spieler == 'R')
		{
			spieler = 'S';
		} else
		{
			spieler = 'R';
		}
	}

	public char winner()
	{

		// Check vertikal
		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j <= (board[i].length - 5); j++)
			{
				boolean match = true;
				char compare = board[i][j];
				for (int k = j + 1; k < j + 5; k++)
				{
					if (board[i][k] != compare)
					{
						match = false;
						break;
					}
				}
				if (match)
				{
					winner = compare;
					return winner;
				}
			}
		}

		// Check horizontal
		for (int i = 0; i <= (board.length - 5); i++)
		{
			for (int j = 0; j < board[i].length; j++)
			{
				boolean match = true;
				char compare = board[i][j];
				for (int k = i + 1; k < i + 5; k++)
				{
					if (board[k][j] != compare)
					{
						match = false;
						break;
					}
				}
				if (match)
				{
					winner = compare;
					return winner;
				}
			}
		}
		winner = ' ';
		return winner;
	}

	public boolean readPosition() throws IOException
	{

		Scanner console = new Scanner(System.in);
		System.out.print(spieler + ":Zeile:");
		int zeile = console.nextInt();
		if (zeile < 1 || zeile > 6)
		{
			System.out.println("Falsche Eingabe");
			return false;

		}
		zeile -= 1;

		System.out.print(spieler + ":Spalte:");
		int spalte = console.nextInt();

		if (spalte < 1 || spalte > 6)
		{
			System.out.println("Falsche Eingabe");
			return false;
		}
		spalte -= 1;

		if (board[zeile][spalte] != ' ')
		{
			System.out.println("Feld bereits belegt.");
			return false;
		}
		this.zeile = zeile;
		this.spalte = spalte;
		return true;

	}

	public boolean readTurn() throws IOException
	{

		Scanner console = new Scanner(System.in);

		System.out.print(spieler + ":Feld:");
		int feld = console.nextInt();
		int felder = (board.length / 3) * 2;
		if (feld < 1 || feld > felder)
		{
			System.out.println("Falsche Eingabe");
			return false;

		}

		System.out.print(spieler + ":Richtung (L|R):");
		String input = console.next();
		char direction = input.length() > 0 ? input.charAt(0) : ' ';
		
		if (direction > 96)
			direction -= 32;

		if (direction != 'L' && direction != 'R')
		{
			System.out.println("Falsche Eingabe");
			return false;

		}

		feld -= 1;
		
		
		this.feld = feld;
		this.direction = direction;

		return true;

	}

	public static void main(String[] args) throws IOException
	{

		PentagoModel game = new PentagoModel();
		System.out.println("Geben Sie die Zeile und Spalte an, die Sie belegen möchten. Anschließend wählen Sie das zu drehende Feld");
		while (game.winner() == ' ')
		{
			System.out.println("Spieler " + game.spieler + " Sie sind dran:");
			do
			{
				game.displayBoard();
			} while (game.readPosition() == false);
			game.setPosition();
			
			do
			{
				game.displayBoard();
			} while (game.readTurn() == false);
			game.turnField();
			
			game.nextPlayer();
		}
		game.displayBoard();
		System.out.println("Gratulation! Gewinner ist " + game.winner);

	}

}
 
wow, echt vielen vielen dank für deine mühe ariol das war doch nicht nötig 😀 ich schaus mir an

@marco: ja, habe die for schleifen geändert
 

Neue Themen


Zurück
Oben