# Vier Gewinnt-Spiel



## salexal (14. Dez 2009)

Hallo!

Ich soll vier gewinnt programmieren, habe als ansatz schon die ganzen methoden und das spielfeld wird auch schon ausgegeben aber ich komme einfach nicht weiter, bitte um Hilfe.


```
public class viergewinnt {

	public static void main(String[] args) {
		char[][] field;
		boolean player1;
		int xPos;
		int yPos;
		boolean endOfGame;
		boolean placed;
		char choice;
		
		field = new char[7][6];
		
		do {
			resetField(field);
			endOfGame = false;
			player1 = true;

			do {		
				// 1. show field
				printField(field);
				
				do {
					printCurrentPlayer(player1);
					Out.println();
					
				    // 2. Read position from player and convert to array coordinates
					xPos = readXPos() - 1;
					yPos = readYPos() - 1;
					
					// 3. Place stone
					placed = placeStone(field, xPos, yPos, player1);
				} while (!placed);
					
				// 4. Test for win or draw
				endOfGame = playerWon(field, player1, xPos, yPos) || isDraw(field);
				
				if (!endOfGame) {
					player1 = !player1;
				}
				
			} while (!endOfGame);
			
			if (playerWon(field, player1, xPos, yPos)) {
				printCurrentPlayer(player1);
				Out.println(" has won!");			
			} else {
				Out.println("Draw!");
			}
			
			printField(field);
			
			do {
				Out.print("Another game? (n/y) ");
				choice = In.readChar();
			} while (choice != 'N' && choice != 'n' && choice != 'y' && choice != 'Y');
			
		} while (choice == 'y' || choice == 'Y');
	}

	static void printCurrentPlayer(boolean player1) {
		if (player1) {
			Out.print("Player 1");
		} else {
			Out.print("Player 2");			
		}
	}

	static void printField(char[][] field) {
		for (int y = 0; y < field[0].length; y++) {
			Out.print("|");
			for (int x=0; x < field.length; x++) {
				Out.print(field[x][y] + "|");
			}
			Out.println();
		}
		for(int i = 0; i < field.length; i++) {
			Out.print("+-");
		}
		Out.println("+");
		Out.println(" 1 2 3 4 5 6 7 ");
	}
	
	static void resetField(char[][] field) {
		for (int y = 0; y < field[0].length; y++) {
			for (int x=0; x < field.length; x++) {
				field[x][y] = ' ';
			}
		}
	}
	
	static int readXPos() {
		int result;
		Out.print("X: ");
		result = In.readInt();
		return result;
	}
	
	static int readYPos() {
		int result;
		Out.print("Y: ");
		result = In.readInt();
		return result;		
	}
	
	static boolean placeStone(char[][] playField, int xPos, int yPos, boolean player1) {
		boolean placeable;
		
		placeable = isValidPosition(playField, xPos, yPos);
		
		if (placeable) {
			playField[xPos][yPos] = symbolForPlayer(player1);
		}
		
		return placeable;
	}
	
	static boolean isValidPosition(char[][] field, int xPos, int yPos) {
		 return isInField(field, xPos, yPos) && field[xPos][yPos] == ' ';
	}
	
	static boolean isInField(char[][] field, int xPos, int yPos) {
		return (xPos >= 0 && xPos < field.length && 
			    yPos >= 0 && yPos < field[0].length);
	}
	
	static char symbolForPlayer(boolean player1) {
		if (player1) {
			return 'X';
		} else {
			return 'O';
		}
	}
	
	static boolean playerWon(char[][] field, boolean player1, int xPos, int yPos) {
		return 
			playerWonHorizontal(field, player1, yPos) ||
			playerWonVertical(field, player1, xPos) ||
			playerWonDiagonal1(field, player1, xPos, yPos) ||
			playerWonDiagonal2(field, player1, xPos, yPos);
	}
		
	private static boolean playerWonVertical(char[][] field, boolean player1,
			int xPos) {
		int count;
		
		count = 0;
		
		for (int y=0; y<field.length; y++) {
			if (field[xPos][y] == symbolForPlayer(player1)) {
				count++;
			}
		}
		
		return count >= 3;
	}

	private static boolean playerWonHorizontal(char[][] field, boolean player1,
			int yPos) {
		int count;
		
		count = 0;
		
		for (int x=0; x<field.length; x++) {
			if (field[x][yPos] == symbolForPlayer(player1)) {
				count++;
			}
		}
		
		return count >= 3;
	}

	private static boolean playerWonDiagonal1(char[][] field, boolean player1,
			int xPos, int yPos) {
		int count;
		
		count = 0;
		
		for (int i=-2; i<=2; i++) {
			if (isInField(field, xPos+i, yPos+i) && 
					field[xPos+i][yPos+i] == symbolForPlayer(player1)) {
				count++;
			}
		}
		
		return count >= 3;
	}
	
	private static boolean playerWonDiagonal2(char[][] field, boolean player1,
			int xPos, int yPos) {
		int count;
		
		count = 1;
		
		for (int i=1; i<3; i++) {
			if ((isInField(field, xPos+i, yPos-i) && 
					field[xPos+i][yPos-i] == symbolForPlayer(player1)) ||
					(isInField(field, xPos-i, yPos+i) && 
					field[xPos-i][yPos+i] == symbolForPlayer(player1))) {
				count++;
			}
		}
		
		return count >= 3;
	}

	static boolean isDraw(char[][] field) {
		for (int y = 0; y < field[0].length; y++) {
			for (int x=0; x < field.length; x++) {
				if (field[x][y] == ' ') {
					return false;
				}
			}
		}
		return true;
	}
	
}
```


DANKE Lg


----------



## Final_Striker (14. Dez 2009)

vielleicht solltest du auch noch sagen, wo genau du nicht weiter kommst ;-)


----------



## salexal (14. Dez 2009)

Ich weiß nicht was in die Methode placeStone gehört damit das Zeichen (X oder O) dann im Feld an der eingegebenen Stelle ausgegeben wird.

Hat jemand eine Idee? Bitte..

lg


----------



## javimka (14. Dez 2009)

Die Methode placeStone hast du doch beretis ausgeschrieben ???:L


----------

