2 years ago
#58837
Sarah Mcwilliams
trying to loop or create a new board- tic tac toe java program
preface: I'm a beginner so the solution may be obvious and I'm not seeing it. I am making a tic tac toe program and I want to make it where the game can be played over and over. I have an idea of how I think it should go but I'm not sure how to implement it. any help or criticism is welcome. (I am trying to repeat it using the switch case at the very end) FULL CODE:
import java.util.*;
import java.util.Random;
public class Main {
static String[] board;
static String turn;
static int counterX=0;
static int counterO=0;
static String continuePlaying;
static String checkWinner()
{
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}
// For O winner
else if (line.equals("OOO")) {
return "O";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(
String.valueOf(a + 1))) {
break;
}
else if (a == 8) {
return "draw";
}
}
// To enter the X Or O at the exact place on board.
System.out.println(
turn + "'s turn; enter a slot number to place "
+ turn + " in:");
return null;
}
static void printBoard()
{
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " | "
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " | "
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
board = new String[9];
final int min=0;
final int max=1;
int num = (int)Math.floor(Math.random()*(max-min+1)+min);
//random number between 0-1
if (num==min){
System.out.println(
"X will play first. Enter a slot number to place X in:");
turn="X";}else{
System.out.println(
"O will play first. Enter a slot number to place O in:");
turn="O";
}
String winner = null;
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
System.out.println("Welcome to 3x3 Tic Tac Toe.");
printBoard();
while (winner == null) {
int numInput;
// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
// This game has two player x and O.
// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}
printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}
if (winner.equalsIgnoreCase("draw")) {
System.out.println(
"It's a draw! Thanks for playing.");
}
else {
System.out.println(
"Congratulations! " + winner
+ "'s have won! Thanks for playing.");
}
if (winner=="X") {
counterX++;
System.out.println("X's have "+ counterX + "(s) wins.");
System.out.println("Os have "+ counterO + "(s) wins.");
}
else if (winner=="O"){
counterO++;
System.out.println("X's have "+ counterX + "(s) wins.");
System.out.println("Os have "+ counterO + "(s) wins.");
}
System.out.println("Continue playing? Enter 1 to continue or 0 to end.");
int play=in.nextInt();
switch(play) {
case 0:
continuePlaying = "N";
java.lang.System.exit(0);
break;
case 1:
continuePlaying = "Y";
break;
}
}
}
java
tic-tac-toe
0 Answers
Your Answer