-
-
Save Wenox/03cf0dc45aef0bb22820e579659bbf9a to your computer and use it in GitHub Desktop.
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
import java.util.Scanner; | |
class InputManager { | |
private static final Scanner sc = new Scanner(System.in); | |
public static String inString() { | |
return sc.next(); | |
} | |
public static int inFieldChoice() { | |
return sc.nextInt(); | |
} | |
} |
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
class Player { | |
private static int movesNum; | |
private final String mark; | |
private String name; | |
public Player(String mark) { | |
this.mark = mark; | |
} | |
public void setName() { | |
this.name = InputManager.inString(); | |
} | |
public String getName() { | |
return this.name; | |
} | |
public String getMark() { | |
return this.mark; | |
} | |
public static int getMovesNum() { | |
return movesNum; | |
} | |
public static void incrementMovesNum() { | |
movesNum++; | |
} | |
} |
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
class Board { | |
private final String[] field; | |
public Board() { | |
field = new String[9]; | |
for (int i = 0; i < 9; i++) { | |
field[i] = String.valueOf(i + 1); | |
} | |
} | |
public void setField(Player player, int choice) { | |
field[choice - 1] = player.getMark(); | |
} | |
public boolean isFieldOccupied(int choice) { | |
return field[choice - 1].equals("X") || field[choice - 1].equals("O"); | |
} | |
public void printBoard() { | |
for (int i = 0; i < 9; i++) { | |
System.out.print(field[i]); | |
if ((i + 1) % 3 == 0) { | |
System.out.print("\n"); | |
} | |
} | |
} | |
public String getField(int i) { | |
return this.field[i]; | |
} | |
} |
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
class Game { | |
private static Player playerA; | |
private static Player playerB; | |
private static Board board; | |
private Game() { | |
} | |
public static void initGame() { | |
playerA = new Player("X"); | |
playerA.setName(); | |
playerB = new Player("O"); | |
playerB.setName(); | |
board = new Board(); | |
board.printBoard(); | |
} | |
public static void runGameLoop() { | |
Player curPlayer = playerA; | |
Player prevPlayer = playerB; | |
while (!isGameFinished(prevPlayer)) { | |
promptToMove(curPlayer); | |
makeMove(curPlayer, board); | |
board.printBoard(); | |
// to be replaced with PlayerSwapper helper class | |
Player temp = curPlayer; | |
curPlayer = prevPlayer; | |
prevPlayer = temp; | |
} | |
} | |
private static void promptToMove(Player player) { | |
System.out.print(player.getName() + "[" + player.getMark() + "]'s turn to move: "); | |
} | |
private static void makeMove(Player player, Board board) { | |
boolean isValidMove = false; | |
while (!isValidMove) { | |
int choice = InputManager.inFieldChoice(); | |
try { | |
if (!board.isFieldOccupied(choice)) { | |
board.setField(player, choice); | |
Player.incrementMovesNum(); | |
isValidMove = true; | |
} | |
else { | |
System.out.println("Field already occupied. Choose new field"); | |
} | |
} | |
catch (ArrayIndexOutOfBoundsException e) { | |
System.out.println("Chosen field is out of scope. Try again"); | |
} | |
} | |
} | |
private static boolean isDraw() { | |
return Player.getMovesNum() >= 9; | |
} | |
private static boolean isWon(Player player) { | |
String playerMark = player.getMark(); | |
for (int i = 0; i <= 6; i += 3) { | |
if (board.getField(i).equals(playerMark) && board.getField(i + 1).equals(playerMark) && board.getField(i + 2).equals(playerMark)) | |
return true; | |
} | |
for (int i = 0; i <= 2; i++) { | |
if (board.getField(i).equals(playerMark) && board.getField(i + 3).equals(playerMark) && board.getField(i + 6).equals(playerMark)) | |
return true; | |
} | |
return (board.getField(0).equals(playerMark) && board.getField(4).equals(playerMark) && board.getField(8).equals(playerMark)) || | |
(board.getField(2).equals(playerMark) && board.getField(4).equals(playerMark) && board.getField(6).equals(playerMark)); | |
} | |
private static boolean isGameFinished(Player player) { | |
if (isDraw()) { | |
System.out.println("Draw!"); | |
return true; | |
} | |
if (isWon(player)) { | |
System.out.println(player.getName() + "[" + player.getMark() + "] has won"); | |
return true; | |
} | |
return false; | |
} | |
} |
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
class Main { | |
public static void main(String[] args) { | |
Game.initGame(); | |
Game.runGameLoop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment