Question Details

(solution) In this lab you will modify the TicTacToe program developed in


In this lab you will modify the TicTacToe program developed in class so as to have the machine player act less idiotically. Get the program developed in class frommoodle.


Step 1.Modify the existing program so that the first thing the machine player attempts to do is block an immediate winning move by the human player. If the machine player detects that no such move exists, then a random move should be made as before.


Step 2.Modify the program again, so that instead of the random move above, if the machine has a clever move to make, the machine makes that move. A clever move is one where no matter where the human player moves, the machine can win on the following move. If no clever move is possible, then a random move should be made as above.


Submit TicTacToe.java


import java.util.Scanner;

 

import java.io.PrintStream;

 

import java.util.Random;

 

public class TicTacToe

 

{

 

static Scanner stdin = new Scanner( System.in);

 

static PrintStream stdout = System.out;

 

public static void main(String arg)

 

{

 

Board bd = new Board();

 

char currentPlayer = 'X';

 

while(true) {

 

getmove(bd, currentPlayer); //puts move on the board

 

bd.print();

 

if( bd.won(currentPlayer)) {

 

stdout.printf("Player %c has won!

 

", currentPlayer);

 

return; //end main

 

}

 

if( bd.done()) {

 

stdout.println("Stalemate!");

 

return;

 

} } if( currentPlayer == 'X')

 

currentPlayer = 'O';

 

else

 

currentPlayer = 'X'; }

 

static void getmove( Board bd, char player) {

 

if( player == 'X') {

 

usermove(bd);

 

} else {

 

machinemove(bd);

 

}

 

}

 

static Random r = new Random(99);

 

static void machinemove(Board bd) {

 

int row = r.nextInt(3);

 

int col = r.nextInt(3);

 

while( !bd.play(row, col, 'O')) {

 

row = r.nextInt(3);

 

col = r.nextInt(3);

 

}

 

}

 

static void usermove(Board bd) {

 

int row = getpos("row (1,2,3): ");

 

int col = getpos("col (1,2,3): ");

 

while( !bd.play(row, col, 'X')) {

 

stdout.println("Cannot play on non-blank location!"); row = getpos("row (1,2,3): ");

 

col = getpos("col (1,2,3): ");

 

}

 

}

 

static int getpos(String prompt) {

 

int i;

 

stdout.print(prompt);

 

try {

 

i = stdin.nextInt();

 

}

 

catch( Exception e) {

 

return getpos(prompt);

 

}

 

if( 1 <= i && i <= 3)

 

return i - 1; //our coords in range 0..2

 

return getpos(prompt);

 

} } class Board {

 

char b;

 

int moves;

 

boolean done() {

 

return moves == 9;

 

}

 

boolean won(char p) { //either 'X' or 'O'

 

for( int i = 0; i < 3; ++i)

 

if( (eq(i,0,p) && eq(i,1,p) && eq(i,2,p)) ||

 

(eq(0,i,p) && eq(1,i,p) && eq(2,i,p))

 

) return true;

 

if( (eq(0,0,p) && eq(1,1,p) && eq(2,2,p)) ||

 

(eq(0,2,p) && eq(1,1,p) && eq(2,0,p))

 

) return true;

 

return false;

 

}

 

private boolean eq(int i, int j, char p) {

 

return b[i][j] == p;

 

}

 

Board() {

 

b = new char[3][3];

 

for( int r = 0; r < 3; ++r)

 

for( int c = 0; c < 3; ++c)

 

b[r][c] = ' ';

 

}

 

boolean play(int row, int col, char p) {

 

if( b[row][col] == ' ') {

 

b[row][col] = p;

 

++moves;

 

return true;

 

} else return false; } } void print() {

 

System.out.println();

 

System.out.printf( " %c | %c | %c

 

", b[0][0], b[0][1], b[0][2]);

 

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

 

System.out.printf( " %c | %c | %c

 

", b[1][0], b[1][1], b[1][2]);

 

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

 

System.out.printf( " %c | %c | %c

 

", b[2][0], b[2][1], b[2][2]);

 

System.out.println();

 

}

 


Solution details:
STATUS
Answered
QUALITY
Approved
ANSWER RATING

This question was answered on: Jan 30, 2021

PRICE: $15

Solution~0001017653.zip (25.37 KB)

Buy this answer for only: $15

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free solution (Deadline assured. Flexible pricing. TurnItIn Report provided)

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .
SiteLock

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Jan 30, 2021

EXPERT

Tutor

ANSWER RATING

GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

NEW ASSIGNMENT HELP?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN A DEADLINE.

Order Now