diff --git a/1710991761/Board.java b/1710991761/Board.java new file mode 100644 index 0000000..1ae4ab0 --- /dev/null +++ b/1710991761/Board.java @@ -0,0 +1,66 @@ +public class Board { + private static char[][] board; + private static int boardSize = 3; + private char p1Symbol1,p2Symbol2; + private int count; + public final static int PLAYER_1_WINS = 1; + public final static int PLAYER_2_WINS = 2; + public final static int DRAW = 3; + public final static int INCOMPLETE = 4; + public final static int INVALID = 5; + + + public Board(char p1Symbol1,char p2Symbol2) { + board = new char[boardSize][boardSize]; + for(int i=0;i=boardSize || y<0 || y>=boardSize || board[x][y]!=' ') { + return INVALID; + } + board[x][y] = symbol; + count++; + //check row + if(board[x][0] == board[x][1] && board[x][0] == board[x][2]) + { + return symbol==p1Symbol1?PLAYER_1_WINS:PLAYER_2_WINS; + } + //check col + if(board[0][y] == board[1][y] && board[0][y] == board[2][y]) + { + return symbol==p1Symbol1?PLAYER_1_WINS:PLAYER_2_WINS; + } + //check first diag + if(board[0][0]!=' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) + { + return symbol==p1Symbol1?PLAYER_1_WINS:PLAYER_2_WINS; + } + //check second diag + if(board[0][2]!=' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) + { + return symbol==p1Symbol1?PLAYER_1_WINS:PLAYER_2_WINS; + } + if(count==boardSize*boardSize) + return DRAW; + + return INCOMPLETE; + } +} \ No newline at end of file diff --git a/1710991761/Manager.java b/1710991761/Manager.java new file mode 100644 index 0000000..96aedf2 --- /dev/null +++ b/1710991761/Manager.java @@ -0,0 +1,82 @@ +import java.util.Scanner; + +public class Manager { + private Player player1, player2; + private Board board; + + private Player takeInput(int num) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter player " + num + " name: "); + String name = sc.nextLine(); + System.out.println("Enter symbol " + num + " symbol: "); + char symbol = sc.next().charAt(0); + Player p = new Player(name, symbol); + return p; + } + + public void startGame() { + //player input, create board, conduct game + Scanner sc = new Scanner(System.in); + player1 = takeInput(1); + player2 = takeInput(2); + while(player1.getSymbol()==player2.getSymbol()) + { + System.out.println("Symbol already taken, Pick another symbol"); + player2.setSymbol(sc.next().charAt(0)); + } + + //make board + board = new Board(player1.getSymbol(), player2.getSymbol()); + int status = Board.INCOMPLETE; + //conduct game + boolean player1Turn = true; + while(status == Board.INCOMPLETE || status == Board.INVALID) + { + if(player1Turn) + { + System.out.println("Player 1 :" + player1.getName()+"'s turn"); + System.out.println("Enter X: "); + int x = sc.nextInt(); + System.out.println("Enter Y: "); + int y = sc.nextInt(); + status = board.move(player1.getSymbol(),x,y); + //1 - player1 win.. 2- player 2 win.. 3- draw.. 4- incomplete.. 5-invalid + if(status!=board.INVALID) + { + player1Turn = false; + board.print(); + }else + System.out.println("Invalid Move, Try again!"); + } + + else { + System.out.println("Player 2 : " + player2.getName()+"'s turn"); + System.out.println("Enter X coord: "); + int x = sc.nextInt(); + System.out.println("Enter Y coord: "); + int y = sc.nextInt(); + status = board.move(player2.getSymbol(),x,y); + //1 - player1 win.. 2- player 2 win.. 3- draw.. 4- incomplete.. 5-invalid + if(status!=board.INVALID) + { + player1Turn = true; + board.print(); + }else + System.out.println("Invalid Move, Try again!"); + + } + + } + if(status==board.PLAYER_1_WINS) + System.out.println("Player 1 - "+player1.getName()+" wins!!"); + else if(status==board.PLAYER_2_WINS) + System.out.println("Player 2 - "+player2.getName()+" wins!!"); + else + System.out.println("Game Draw"); + + } + public static void main(String[] args) { + Manager m = new Manager(); + m.startGame(); + } +} \ No newline at end of file diff --git a/1710991761/Player.java b/1710991761/Player.java new file mode 100644 index 0000000..ae5163b --- /dev/null +++ b/1710991761/Player.java @@ -0,0 +1,25 @@ +public class Player { + private String name; + private char symbol; + + public Player(String name,char symbol) + { + setName(name); + setSymbol(symbol); + } + public String getName() { + return name; + } + public void setName(String name) { + if(!name.isEmpty()) + this.name = name; + } + public char getSymbol() { + return symbol; + } + public void setSymbol(char symbol) { + if(symbol!='\0') + this.symbol = symbol; + } + +} \ No newline at end of file