Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have uploaded all necessary source codes for the game in belarn). It is a fullfunctioning program. You can run the game as it is

I have uploaded all necessary source codes for the game in belarn). It is a fullfunctioning program. You can run the game as it is by executing the code from the TicTacToe.java file. I have provided the source codes for this lab. The reason for providing the source codes are as follows: - In real-life you will face some situations, when you will be given some existing codes to work with and then you will need to update/modify them to meet the future requirements. - If you see how other people have applied OOP concept on a large complex problem, then that experience will help you to apply the OOP concepts in your project (coming next). In other words, these source codes should give you an idea that how Tic-Tac-Toe game app is modularized into small independent classes (modules) and how these classes interact with each other to build a complex game app like Tic-Tac-Toe. Once you study all the source codes and understand their functions and relations, then, modify the game so that a player can play against the computer. In other words, modify the provided tic-tac-toe game, so that the computer plays the role of the player CROSS (X), and automatically plays against the other player (O). BONUS (5 points): If you could make the brain of the computer smart enough that it never loses in the game. Submission for Exercise 2: Submit all the source code files. Highlight all the parts that you have modified from the original code. Comment the source code properly. Marks will be deducted if the source code is not properly commented. Test and take some snap-shots of your test and submit that as well.

public class Board { public final int ROWS = 3; public final int COLS = 3; private char b[][]; public Board() { this.b = new char[ROWS][COLS]; } public boolean isFull(){ return false; } public void clear(){ for(int i=0; i=0 && r =0 && c 

import java.util.Scanner; /** * Created */ public class GameController { // Game states private final int PLAYING = 0; private final int DRAW = 1; private final int X_WIN = 2; private final int O_WIN = 3; // Players private final char CROSS = 'x'; private final char NOUGHT = '0'; private int currentRow, currentCol; private int gameState; private char currentPlayer; private Board board; private static Scanner kb; public GameController() { this.gameState = PLAYING; board = new Board(); kb = new Scanner(System.in); } private void playerMove(char curPlayer){ int row, col; while(true) { if (curPlayer == CROSS) System.out.println("Player X, please enter a valid move [row[0-2] col(0-2)]: "); else System.out.println("Player O, please enter a valid move [row[0-2] col(0-2)]: "); row = kb.nextInt(); col = kb.nextInt(); boolean valid = board.placeASymbol(curPlayer, row, col); if (!valid) { System.out.println("Invalid move; please try again!"); continue; } break; } currentRow = row; currentCol = col; } private void updateState(){ //System.out.println(currentRow + "," + currentCol); if(board.hasWon(CROSS, currentRow, currentCol)) gameState = X_WIN; else if(board.hasWon(NOUGHT, currentRow, currentCol)) gameState = O_WIN; else if(board.isDraw()) gameState = DRAW; } private void changeTurn(char curPlayer){ if(curPlayer == CROSS) currentPlayer = NOUGHT; else currentPlayer = CROSS; } public void startGame(){ board.clear(); currentPlayer = CROSS; gameState = PLAYING; board.display(); while(true) { playerMove(currentPlayer); board.display(); updateState(); if (gameState == X_WIN) { System.out.println("X won the game! bye!"); break; } else if (gameState == O_WIN) { System.out.println("O won the game! bye!"); break; } else if (gameState == DRAW) { System.out.println("It's a draw! Bye!"); break; } changeTurn(currentPlayer); } } } 

public class TicTacToe { public static void main(String[] args) { GameController gc = new GameController(); gc.startGame(); } } 

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_2

Step: 3

blur-text-image_3

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

Genetic Databases

Authors: Martin J. Bishop

1st Edition

0121016250, 978-0121016258

More Books

Students also viewed these Databases questions

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

a. How are members selected to join the team?

Answered: 1 week ago