Question
I want to know if there is a way to change a few things in my code to get my ai to work becuase right
I want to know if there is a way to change a few things in my code to get my ai to work becuase right now it just keeps skipping over my ai player and if so show how thanks!!!!!
//Test class
package tictactoe;
import java.util.Scanner;
/** * * */ public class Practice { public static void main(String[] args) { Scanner scan = new Scanner(System.in); tictactoemain game = new tictactoemain();
System.out.println("TicTacToe"); do{ game.designboard(); int row; int col; System.out.println("Player enter a number between 1-3 for for row and again for column - "); row = scan.nextInt() - 1; col = scan.nextInt() - 1;
while (!game.placement(row, col)); game.changePlayer(); game.getaiplayer(); } while (!game.checkForWin() && !game.BoardFull()); if (game.BoardFull() && !game.checkForWin()) { System.out.println("Draw"); } else { System.out.println("current board layout:"); game.makeboard(); game.changePlayer(); System.out.println(Character.toUpperCase(game.getplayer()) + "Wins!");
}
} }
//My main work
package tictactoe;
/** * * */ public class tictactoemain { private char[][] board; private char player = 'x'; private char aiplayer = 'o';
public tictactoemain(){ board = new char[3][3]; makeboard(); } //design the board and its variables public void makeboard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } } public void designboard() { System.out.print("--------- "); for (int i = 0; i < 3; i++) { System.out.print("|"); for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + "|"); } System.out.println(); System.out.println("--------"); } } public boolean BoardFull() { boolean isFull = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == '-') { isFull = false; } } } return isFull; } //end of making the board while checking if its full //check to see if there is a winner in each row and col public boolean checkForWin() { return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin()); } private boolean checkRowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) { return true; } } return false; } private boolean checkColumnsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) { return true; } } return false; } private boolean checkDiagonalsForWin() { return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true)); } private boolean checkRowCol(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } public void changePlayer() { if (player == 'x') { aiplayer = 'o'; } else { player = 'x'; } } public boolean getaiplayer(int row, int col){ for(int i=0;i<3;i++){ if(board[row][col] == '-'){ System.out.print(1); System.out.print(1); } } return true; } public char getaiplayer() { return player; } public char getplayer() { return player; } public boolean placement(int row, int col) { if ((row >= 0) && (row < 3)) { if ((col >= 0) && (col < 3)) { if (board[row][col] == '-') { board[row][col] = player; return true; } } } return false; } }
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