Question: In the TicTacToe application, the computers selection is chosen randomly. Improve the TicTacToe game so that when the computer has two Os in any row,
In the TicTacToe application, the computers selection is chosen randomly.
Improve the TicTacToe game so that when the computer has two Os in any row, column, or diagonal, it selects the winning position for its next move rather than selecting a position randomly. Save the improved game as TicTacToe2.java.
Original code:
Game:
package edu.snhu.it145;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Game {
public void playGame()
{
TicTacToe game1 = new TicTacToe();
Random rn1 = new Random();
int rangeval = 9 - 1 + 1;
Map
game1.setPlayer11("you");
game1.setPlayer22("System");
game1.setMarker11('O');
game1.setMarker22('X');
boolean continuePlayingS = true;
continuePlayingS = true;
while (continuePlayingS) {
game1.init();
System.out.println(game1.drawBoardB());
System.out.println();
String playerVal = null;
while (!game1.winnerW() && game1.getPlaysOF() < 9) {
playerVal = game1.getCurrentPlayerOF() == 1 ?
game1.getPlayer11(): game1.getPlayer22();
boolean validPickS = false;
while (!validPickS) {
int pick1 = 0;
if (game1.getCurrentPlayerOF() != 2) {
System.out.print("It " + playerVal +
"r's turn. Pick a square: ");
String squareVal = game1.getPromptOf();
if (squareVal.length() == 1&&
Character.isDigit(squareVal.toCharArray()[0])) {
try {
pick1 = Integer.parseInt(squareVal);
map.put(pick1, null);
} catch (NumberFormatException e) {
}
}
validPickS = game1.placeMarkerM(pick1);
} else {
System.out.print("It " + playerVal+ "'s turn now: ");
do {
int randOd = rn1.nextInt(rangeval)
+ 1;
if (!map.containsKey(randOd)) {
validPickS =
game1.placeMarkerM(randOd);
}
} while (!validPickS);
}
if (!validPickS) {
System.out.println("Square can not be selected. Retry");
}
}
game1.switchPlayersOF();
System.out.println();
System.out.println(game1.drawBoardB());
System.out.println();
}
if (game1.winnerW()) {
System.out.println("Game Over - " + playerVal +
" WINS!!!");
} else {
System.out.println("Game Over - Draw");
}
System.out.println();
System.out.print("Play again? (Y/N): ");
String choiceval = game1.getPromptOf();
if (!choiceval.equalsIgnoreCase("Y")) {
continuePlayingS = false;
}
}
}
public static void main(String[] args)
{
Game mainVal = new Game();
mainVal.playGame();
}
}
Toctactoe:
package edu.snhu.it145;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//class name tic tac toe class
/**
* @author dylan.amazeen
*main class that declares variables
*/
public class TicTacToe
{
//declaring variables
private char[][] boardOf = new char[3][3];
private String playerP1;
private String playerP2;
private int currentPlayerC;
private char markerM1;
private char markerM2;
private int playsS;
private BufferedReader readerValue = new BufferedReader(new InputStreamReader(System.in));
protected void init() {
int counterVal = 0;
for (int a = 0; a < 3; a++) {
for (int a1 = 0; a1 < 3; a1++) {
boardOf[a][a1] = Character.forDigit(++counterVal, 10);
}
}
currentPlayerC = 1;
playsS = 0;
}
protected void switchPlayersOF() {
if (getCurrentPlayerOF() == 1) {
setCurrentPlayerOF(2);
} else {
setCurrentPlayerOF(1);
}
setPlaysOf(getPlaysOF() + 1);
}
protected boolean placeMarkerM(int playY) {
for (int a = 0; a < 3; a++) {
for (int a1 = 0; a1 < 3; a1++) {
if (boardOf[a][a1] == Character.forDigit(playY, 10)) {
boardOf[a][a1] = (getCurrentPlayerOF() == 1) ? getMarker11() :getMarker22();
return true;
}
}
}
return false;
}
//calculates winner
protected boolean winnerW() {
// Checking row
char currentval = ' ';
for (int a = 0; a < 3; a++) {
int a1 = 0;
for (a1 = 0; a1 < 3; a1++) {
if (!Character.isLetter(boardOf[a][a1])) {
break;
}
if (a1 == 0) {
currentval = boardOf[a][a1];
} else if (currentval != boardOf[a][a1]) {
break;
}
if (a1 == 2) {
// Found winner
return true;
}
}
}
for (int a = 0; a < 3; a++) {
currentval = ' ';
int a1 = 0;
for (a1 = 0; a1 < 3; a1++) {
if (!Character.isLetter(boardOf[a1][a])) {
break;
}
if (a1 == 0) {
currentval = boardOf[a1][a];
} else if (currentval != boardOf[a1][a]) {
break;
}
if (a1 == 2) {
// Found winner
return true;
}
}
}
currentval = boardOf[0][0];
if (Character.isLetter(currentval) && boardOf[1][1] == currentval&& boardOf[2][2] == currentval) {
return true;
}
currentval= boardOf[2][0];
if (Character.isLetter(currentval) && boardOf[1][1] == currentval&& boardOf[0][2] == currentval) {
return true;
}
return false;
}
/**
* @return
* prints the promnt
*/
protected String getPromptOf() {
String promptvalue= "";
try {
promptvalue = readerValue.readLine();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return promptvalue;
}
/**
* @return
* Draws the game board
*/
protected String drawBoardB() {
StringBuilder builderR = new StringBuilder("Game board: ");
for (int a = 0; a < 3; a++) {
for (int a1 = 0; a1 < 3; a1++) {
builderR.append("[" + boardOf[a][a1] + "]");
}
builderR.append(" ");
}
return builderR.toString();
}
//sets and gets
public int getCurrentPlayerOF() {
return currentPlayerC;
}
public void setCurrentPlayerOF(int currentPlayerC) {
this.currentPlayerC = currentPlayerC;
}
public char getMarker11() {
return markerM1;
}
public void setMarker11(char markerM1) {
this.markerM1 = markerM1;
}
public char getMarker22() {
return markerM2;
}
public void setMarker22(char markerM2) {
this.markerM2 = markerM2;
}
public int getPlaysOF() {
return playsS;
}
public void setPlaysOf(int playsS) {
this.playsS= playsS;
}
public String getPlayer11() {
return playerP1;
}
public void setPlayer11(String playerP1) {
this.playerP1 = playerP1;
}
public String getPlayer22() {
return playerP2;
}
public void setPlayer22(String playerP2) {
this.playerP2 = playerP2;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
