Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions

Question

4. Evaluation is ongoing and used to improve the system.

Answered: 1 week ago

Question

6. Effectively perform the managers role in career management.

Answered: 1 week ago