-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TicTacToe Game using core java #311
Comments
plz assign this task to me My ProjectBoard.javaPlayer.javaTicTacToe.javaBoard.java
package tictactoe;
public class Board {
private char[][] board;
private int boardSize = 3;
private char p1Symbol, p2Symbol;
private int count;
public static final int PLAYER1_WIN = 1;
public static final int PLAYER2_WIN = 2;
public static final int DRAW = 3;
public static final int INCOMPLETE = 4;
public static final int INVALID = 5;
public Board(char p1Symbol, char p2Symbol) {
board = new char[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
board[i][j] = ' ';
}
}
this.p1Symbol = p1Symbol;
this.p2Symbol = p2Symbol;
}
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 == p1Symbol ? PLAYER1_WIN : PLAYER2_WIN;
}
// Check column
if (board[0][y] == board[1][y] && board[0][y] == board[2][y]) {
return symbol == p1Symbol ? PLAYER1_WIN : PLAYER2_WIN;
}
// Check diagonal
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return symbol == p1Symbol ? PLAYER1_WIN : PLAYER2_WIN;
}
// Check anti-diagonal
if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
return symbol == p1Symbol ? PLAYER1_WIN : PLAYER2_WIN;
}
if (count == boardSize * boardSize) {
return DRAW;
}
return INCOMPLETE;
}
public void print() {
System.out.println();
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("-------------");
}
}
}
Player.java
package tictactoe;
public class Player {
private String name;
private char symbol;
public Player(String name, char symbol) {
setName(name);
setSymbol(symbol);
}
public void setName(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
}
}
public void setSymbol(char symbol) {
if (symbol != '\0' && symbol != ' ') {
this.symbol = symbol;
}
}
public String getName() {
return this.name;
}
public char getSymbol() {
return this.symbol;
}
}
TicTacToe.java
package tictactoe;
import java.util.Scanner;
public class TicTacToe {
private Player p1, p2;
private Board board;
public void startGame() {
Scanner sc = new Scanner(System.in);
p1 = takePlayerInput(1, sc);
p2 = takePlayerInput(2, sc);
while (p1.getSymbol() == p2.getSymbol()) {
System.out.println("Symbol is already taken! Enter another symbol:");
char symbol = sc.next().charAt(0);
p2.setSymbol(symbol);
}
board = new Board(p1.getSymbol(), p2.getSymbol());
boolean p1Turn = true;
int status = board.INCOMPLETE;
while (status == board.INCOMPLETE || status == board.INVALID) {
if (p1Turn) {
System.out.println("Player 1 " + p1.getName() + "'s Turn!");
} else {
System.out.println("Player 2 " + p2.getName() + "'s Turn!");
}
System.out.println("Enter x:");
int x = sc.nextInt();
System.out.println("Enter y:");
int y = sc.nextInt();
status = (p1Turn) ? board.move(p1.getSymbol(), x, y) : board.move(p2.getSymbol(), x, y);
if (status != board.INVALID) {
p1Turn = !p1Turn;
board.print();
} else {
System.out.println("Invalid move!");
}
}
if (status == board.PLAYER1_WIN) {
System.out.println("Player 1 " + p1.getName() + " is the WINNER!");
} else if (status == board.PLAYER2_WIN) {
System.out.println("Player 2 " + p2.getName() + " is the WINNER!");
} else {
System.out.println("It's a Draw!");
}
}
private Player takePlayerInput(int num, Scanner sc) {
System.out.println("Enter Player " + num + " name:");
String name = sc.nextLine();
System.out.println("Enter Player " + num + " symbol:");
char symbol = sc.next().charAt(0);
sc.nextLine(); // Consume the newline character
return new Player(name, symbol);
}
public static void main(String[] args) {
TicTacToe t = new TicTacToe();
t.startGame();
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please add Hacktober Fest tag
import java.util.HashSet;
import java.util.Scanner;
public class Main {
static char[][] box = new char[3][3];
static String Player1 = "", Player2 = "";
static boolean Player1win = false;
static boolean Player2win = true;
static boolean Draw = false;
static boolean Pending = false;
static int count = 0;
static HashSet display = new HashSet <>();
static HashSet set = new HashSet <>();
public static void main(String[] args) {
}
The text was updated successfully, but these errors were encountered: