-
-
Save Lanero666/9780afe6ab95611b0128a8fe13090e06 to your computer and use it in GitHub Desktop.
krestiki noliki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
board arr = new board('X', new char[3][3], false); | |
arr.inichializeBoard(); | |
arr.printBoard(); | |
while (!arr.isGameWon()) { | |
arr.makeMove(); | |
arr.printBoard(); | |
arr.checkForWin(); | |
arr.changePlayer(); | |
} | |
if (arr.isGameWon()) { | |
if (arr.currentPlayer == 'X') | |
System.out.println("Игрок O Победил"); | |
else if (arr.currentPlayer == 'O') { | |
System.out.println("Игрок X Победил"); | |
}else | |
System.out.println("Ничья"); | |
} | |
} | |
} | |
import java.util.Scanner; | |
public class board { | |
char currentPlayer; | |
char[][] board; | |
public boolean gameWon; | |
public void inichializeBoard() { | |
for (int row = 0; row < 3; row++) { | |
for (int col = 0; col < 3; col++) { | |
board[row][col] = ' '; | |
} | |
} | |
} | |
public void printBoard() { | |
for (int row = 0; row < 3; row++) { | |
for (int col = 0; col < 3; col++) { | |
System.out.print(board[row][col]); | |
if (col < 2) { | |
System.out.print(" | "); | |
} | |
} | |
System.out.println(); | |
if (row < 2) { | |
System.out.println("_____________"); | |
} | |
} | |
} | |
public void makeMove() { | |
Scanner in = new Scanner(System.in); | |
System.out.println("Ход игрока " + currentPlayer + " : "); | |
int row = in.nextInt(); | |
int col = in.nextInt(); | |
while (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') ; | |
board[row][col] = currentPlayer; | |
} | |
public void checkForWin() { | |
for (int i = 0; i < 3; i++) { | |
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer | |
|| board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer | |
|| board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer | |
|| board[2][0] == currentPlayer && board[1][1] == currentPlayer && board[0][2] == currentPlayer) { | |
gameWon = true; | |
} | |
} | |
boolean isFul = true; | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
if (board[i][j] == ' ') { | |
isFul = false; | |
break; | |
} | |
} | |
} | |
if (isFul) | |
gameWon = true; | |
} | |
public void changePlayer() { | |
if (currentPlayer == 'X') { | |
currentPlayer = 'O'; | |
} else { | |
currentPlayer = 'X'; | |
} | |
} | |
public board(char currentPlayer, char[][] board, boolean gameWon) { | |
this.currentPlayer = currentPlayer; | |
this.board = board; | |
this.gameWon = gameWon; | |
} | |
public char getCurrentPlayer() { | |
return currentPlayer; | |
} | |
public void setCurrentPlayer(char currentPlayer) { | |
this.currentPlayer = currentPlayer; | |
} | |
public char[][] getBoard() { | |
return board; | |
} | |
public void setBoard(char[][] board) { | |
this.board = board; | |
} | |
public boolean isGameWon() { | |
return gameWon; | |
} | |
public void setGameWon(boolean gameWon) { | |
this.gameWon = gameWon; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment