Question

I need help with this method (public static int computer_move(int board[][])) . When I run it...

I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already

package tictactoe;

import java.util.Scanner;

public class TicTacToeGame {

static final int EMPTY = 0;

static final int NONE = 0;

static final int PLAYER1 = 1;

static final int COMPUTER = 2;

static final int TIE = 3;

static final int PLAYER2 = 4;

public static void main(String[] args) {

int turn = PLAYER1;

int[][] board = new int[3][3];

int move;

int winner;

int gameChoice=0;

Scanner input=new Scanner(System.in);

System.out.println("**********Welcome to the game of TIC-TAC-TOE**********");

System.out.println("Choose the number 0. for: You Vs. Computer");

System.out.println("Choose the number 1. for: Human Vs. Human");

try { gameChoice = input.nextInt();

}

catch(Exception e){

System.out.println("Type 0. or 1.");

System.exit(0); }

if (gameChoice==0)

System.out.println("Its You Vs. Computer!");

else if (gameChoice==1)

System.out.println("1. User1 Vs. User2");

else { System.out.println("Default: 0");

gameChoice=0;

System.out.println("Its You Vs. Computer!");

}

System.out.println("Enter 1-9 to indicate your move");

System.out.println("1|2|3"); System.out.println("-----");

System.out.println("4|5|6");

System.out.println("-----");

System.out.println("7|8|9");

while(true) { if(turn == PLAYER1) {

if(gameChoice==0)

System.out.println("Your move");

else

System.out.println("User1's move");

move = 0;

while (move<1 || move>9 || board[(move-1)/3][(move-1)%3] != EMPTY) {

System.out.println("Please enter your move(1-9): ");

move = input.nextInt();

}

}

else if(turn == PLAYER2) {

System.out.println("User2's move");

move = 0;

while (move<1 || move>9 || board[(move-1)/3][(move-1)%3] != EMPTY) {

System.out.println("Please enter your move(1-9): ");

move = input.nextInt();

}

}

else {

move = computer_move(board);

System.out.println("Computer move: " + move);

}

board[(int)((move-1)/3)][(move-1)%3] = turn;

print_board(board);

winner = checkWinner(board);

if(winner != NONE) break;

if(turn == PLAYER1)

if (gameChoice==0)

turn = COMPUTER;

else turn = PLAYER2;

else turn = PLAYER1;

}

switch(winner) {

case PLAYER1:

if(gameChoice==0)

System.out.println("You won!");

else

System.out.println("User 1 won!");

break;

case PLAYER2: System.out.println("User 2 won!");

break;

case COMPUTER: System.out.println("Computer won!");

break;

default: System.out.println("Tie!"); break; }

}

public static void print_board(int[][] board) {

System.out.print(printChar(board[0][0]));

System.out.print("|");

System.out.print(printChar(board[0][1]));

System.out.print("|");

System.out.println(printChar(board[0][2]));

System.out.println("-----");

System.out.print(printChar(board[1][0]));

System.out.print("|");

System.out.print(printChar(board[1][1]));

System.out.print("|");

System.out.println(printChar(board[1][2]));

System.out.println("-----");

System.out.print(printChar(board[2][0]));

System.out.print("|");

System.out.print(printChar(board[2][1]));

System.out.print("|");

System.out.println(printChar(board[2][2]));

}

public static char printChar(int b) {

switch(b) {

case EMPTY:

return ' ';

case PLAYER1:

return 'X';

case PLAYER2:

return 'O';

case COMPUTER:

return 'O'; }

return ' '; }

public static int checkWinner(int[][] board) {

if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2]))

return board[0][0];

if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2]))

return board[1][0];

if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2]))

return board[2][0];

if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0]))

return board[0][0];

if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1]))

return board[0][1];

if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2]))

return board[0][2];

if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))

return board[0][0];

if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]))

return board[0][2];

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 TIE; }

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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I need help with this method (public static int computer_move(int board[][])) . When I run it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Hi, I am a student in college who is trying to make a tic tac toe...

    Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT