Write a program (GameOfNim.java) in which the computer plays against a human opp
ID: 3656432 • Letter: W
Question
Write a program (GameOfNim.java) in which the computer plays against a human opponent:Explanation / Answer
import java.util.Scanner; public class NimGame { public static int player = 1;//THIS IS A FIX public static boolean flag = true; public static void main(String[] args) { //Declare variables //TOOK player AWAY HERE AND MADE IT GLOBAL STATIC int pile = 0;//which pile to pick from int stones = 0;//how many stones to pick int counter = 0;//if the game is over or not -> 0 not over int move; //Scanner Scanner input = new Scanner(System.in); NimClass myAssistant = new NimClass(); myAssistant.displayBoard();//board gets set up //Begin loop while(counter == 0) { //Choose pile you want to remove stones from. System.out.print("Player " + player + " Please enter the pile you wish" + " to remove from. (1, 2, 3 or 4) "); pile = input.nextInt();//get a pile number //Enter # of stones you want to remove. System.out.print("Player " + player + " Please enter the amount of" + " stones you wish to remove "); stones = input.nextInt(); myAssistant.playerMove(pile, stones); //Determine what player is entering the values. if (player == 1 && flag == true) { player = 2; } else if(player == 1 && flag == false) { player = 1; flag = true;//set the flag back to true } else if(player == 2 && flag == true) { player = 1; } else { player = 2; flag = true; } //Display board / determine the winner of the game. myAssistant.displayBoard(); myAssistant.determineWinner(); if (myAssistant.determineWinner() != -1) counter = 1; } } } nim class: public class NimClass { //Declare varaiables private int [] _board;//the board for the game -> just an array public NimClass() { //Determing # of stones in each pile at the beginning. _board = new int [4]; _board[0] = 3;//pile 1 - 3 stones _board[1] = 5;//pile 2 - 5 stones _board[2] = 7;//pile 3 - 7 stones _board[3] = 9;//pile 4 - 9 stones System.out.println("Welcome to the Game of Nim " + "You'll need to first pick a pile " + "Then select how many stones you wish to remove "); } //Display the game board public void displayBoard()//just setting up the board with default values { for(int row = 0; row < 4; row++) { if(_board[row] == 0) System.out.print(" 0 "); else System.out.print(" " + _board[row] + " "); } //Print out the piles System.out.println(" Pile 1 Pile 2 Pile 3 Pile 4 "); } //Determine the player's moves public boolean playerMove (int pile, int stones) { if ((pile < 1) || (pile > _board.length)) { return false;//user specified wrong pile } if (_board[pile - 1] /2 >= stones || _board[pile-1] == 1) { _board[pile - 1] -= stones; return true; } else { System.out.println("Invalid number of stones"); NimGame.flag = false; return false; } } //Determine the winner of the game public int determineWinner() { boolean complete = true; int winner = -1; //Checking the piles to see if they're empty. if((_board[0] == 0) && ( _board[1]== 0) && ( _board[2]==0) && ( _board[3]==0)) { winner = NimGame.player;//FIXED THIS HERE complete = false;//MADE CHANGE HERE! } if(complete == false && winner == 1) { //Display the results of player 1 winning. System.out.println("Player 1 Wins!"); winner = 0; } if(complete == false && winner == 2) { //Display the results of player 2 winning. System.out.println("Player 2 Wins!"); winner = 0; } return winner; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.