Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions 1710991761/Board.java
Original file line number Diff line number Diff line change
@@ -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;i++)
for(int j=0;j<boardSize;j++)
board[i][j] = ' ';

this.p1Symbol1 = p1Symbol1;
this.p2Symbol2 = p2Symbol2;
}
public static void print() {
System.out.println("-------------------------------------------------");
for(int i=0;i<boardSize;i++) {
for(int j=0;j<boardSize;j++) {
System.out.print("| "+board[i][j]+" |");
}
System.out.println();
}
System.out.println();
System.out.println("-------------------------------------------------");

}
public int move(char symbol,int x,int y)
{
if(x<0 || x>=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;
}
}
82 changes: 82 additions & 0 deletions 1710991761/Manager.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
25 changes: 25 additions & 0 deletions 1710991761/Player.java
Original file line number Diff line number Diff line change
@@ -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;
}

}