Question
I need this code fixed. i don't why the computer is randomly picking slots for itself and the player too and as soon as i
I need this code fixed. i don't why the computer is randomly picking slots for itself and the player too and as soon as i insert an input. Fix the program to make the player to go first and the X character should be in the right spot and then computer picks. GIVE THE COMPUTER BETTER STRATEGY TO PICK SLOTS!!!!
package TicTacToe;
import hsa.Console;
class Tictac {
static final int EMPTY = 0;
static final int NONE = 0;
static final int Player = 1;
static final int COMPUTER = 2;
static final int STALEMATE = 3;
static Console output;
public static void main(String[] args) {
output = new Console(20,70);
output.setTitle("Introduction to TicTacToe");
// Data objects
// 1 = user, 2 = computer
int turn = Player;
// We will represent the board as nine cells
// 0 = empty, 1 = user, 2 = computer
int[][] board = new int[3][3];
// move: 1-9 representing ul through lr
int move;
// winner: 0 = none, 1 = user, 2 = computer, 3 = stalemate
int winner;
int choice= 0;
// Print Instructions
output.println("--Welcome to Tic-Tac-Joe game--");
output.println("Do want to start First Y/N?");//enter y
output.println("Ok your character is X and computer character is O");
// Print the board
print_board(board);
// While (game not over)
while(true) {
if(turn == Player) {
output.println("Your move");
move = 2;
while (move<0 || move>9 || board[move/3][move%3] != EMPTY) {
output.println("pick your spot: ");
move = choice = output.readInt();
output.readChar();
}
} else {
move = computer_move(board);
output.println("Ok,Computer picks: " + move);
}
// Update the board
board[(int)(move/3)][move%3] = turn;
// Print the board
print_board(board);
// if game is over
winner = checkWinner(board);
if(winner != NONE)
break;
// switch turn
if(turn == Player) {
turn = COMPUTER;
} else {
turn = Player;
}
}
// Print out the outcome
switch(winner) {
case Player:
output.println("You won!");
break;
case COMPUTER:
output.println("Computer won!");
break;
default:
output.println("Tie!");
break;
}
}
// Print the board
public static void print_board(int[][] board) {
output.print(printChar(board[0][0]));
output.print("|");
output.print(printChar(board[0][1]));
output.print("|");
output.println(printChar(board[0][2]));
output.println("-----");
output.print(printChar(board[1][0]));
output.print("|");
output.print(printChar(board[1][1]));
output.print("|");
output.println(printChar(board[1][2]));
output.println("-----");
output.print(printChar(board[2][0]));
output.print("|");
output.print(printChar(board[2][1]));
output.print("|");
output.println(printChar(board[2][2]));
}
// Return an X or O, depending upon whose move it was
public static char printChar(int b) {
switch(b) {
case EMPTY:
return ' ';
case Player:
return 'X';
case COMPUTER:
return 'O';
}
return ' ';
}
// See if the game is over
public static int checkWinner(int[][] board) {
// Check if someone won
// Check horizontals
// top row
if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2]))
return board[0][0];
// middle row
if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2]))
return board[1][0];
// bottom row
if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2]))
return board[2][0];
// Check verticals
// left column
if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0]))
return board[0][0];
// middle column
if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1]))
return board[0][1];
// right column
if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2]))
return board[0][2];
// Check diagonals
// one diagonal
if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
return board[0][0];
// the other diagonal
if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]))
return board[0][2];
// Check if the board is full
if(board[0][0] == EMPTY ||
board[0][1] == EMPTY ||
board[0][2] == EMPTY ||
board[1][0] == EMPTY ||
board[1][1] == EMPTY ||
board[1][2] == EMPTY ||
board[2][0] == EMPTY ||
board[2][1] == EMPTY ||
board[2][2] == EMPTY)
return NONE;
return STALEMATE;
}
// Generate a random computer move
public static int computer_move(int[][] board) {
int move = (int)(Math.random()*9);
while(board[move/3][move%3] != EMPTY)
move = (int)(Math.random()*9);
return move;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started