Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3460:209 PROGRAMMING PROJECT 2: GETTYSBURG DICE chapter1: introduction to comput

ID: 641878 • Letter: 3

Question

3460:209 PROGRAMMING PROJECT 2: GETTYSBURG DICE

chapter1: introduction to computer and programming.

chapter2: introduction to C++

chapter3: expression and interactivity

chapter4: making decision (if statments)

chapter5: loops and files

Please do not use any arrays or anything byond these chapters.

From July 1 to July 3, 1865, the Civil War and the attention of two nations focused on the area near Gettysburg, Pennsylvania. The Confederate Army of Northern Virginia, under the command of General Robert E. Lee, faced off against the Union Army of the Potomac controlled by Major-General George G. Meade. The actual battle, won by Meade, saw 7000 casualties, 32000 wounded soldiers and 10000 taken prisoners or unaccounted for.

The purpose of this assignment is to create a (very) simple simulation which re-enacts the Battle of Gettysburg using coin flips. That said, this is still complicated enough that we should do some high-level planning. Such a road map, known as pseudocode, for this program appears below, as well as an indication of what background you need to finish each individual task. Because we will be constructing this program out-of-order as we build up individual concepts, this outline will help you determine where the individual piece that you

Explanation / Answer

here is a very basic and simple version i wrote. it is working, however i suggest that you try and improve it. import java.util.Random; import java.util.Scanner; public class TheGameofPig { Player player; AIplayer computer; boolean playerTurn; Scanner scanner; private int dice; private volatile boolean isRunning; public TheGameofPig() { player = new Player("Player1"); computer = new AIplayer("Computer"); playerTurn = true; scanner = new Scanner(System.in); isRunning = true; } public void gameLoop(){ while(isRunning){ if(playerTurn){ char decision = playerDecision(); if(decision == 'r'){ if(!(turnLogic(player))){ playerTurn = false; } printPlayerPts(player); checkGameState(player); } else if(decision == 'h'){ playerTurn = false; } } else { if(computer.checkPtsOnRow()){ if(!(turnLogic(computer))){ playerTurn = true; } printPlayerPts(computer); checkGameState(computer); } else{ playerTurn = true; } } } } private void checkGameState(Player p){ //check the winner if(p.getPts() >= 100){ System.out.println(p.getName() +" has won!"); isRunning = false; } } private char playerDecision(){ //scanner for the human player System.out.println(player.getName() +" type 'r' to roll, 'h' to hold."); char c = scanner.nextLine().charAt(0); return c; } private int rollDice(){ //rolling the dice Random randnumber = new Random(); return (int) (randnumber.nextInt(6)) + 1; } private void printDiceResult(Player p){ //print the dice result for player p System.out.println(p.getName() +" got " +dice); } private boolean turnLogic(Player p){ printPlayerPts(p);//print pts of player p at the beginning of a turn dice = rollDice();//roll the dice printDiceResult(p);//print the dice result return p.doLogic(dice);//make the logic of this player } private void printPlayerPts(Player p){ //print pts of player p at the beginning of a turn System.out.println(p.getName() +" has " +p.getPts() +" points"); } public static void main(String[] args){ TheGameofPig game = new TheGameofPig(); game.gameLoop(); } } class Player { private String name; private int pts; public Player(String name){ this.name = name; this.initiatePts(); } public int getPts(){ return this.pts; } private void initiatePts(){ this.pts =0; } private void addPts(int pts){ this.pts+=pts; } public String getName(){ return this.name; } public boolean doLogic(int dice){ if(dice == 1){ this.initiatePts(); return false; } else{ addPts(dice); return true; } } public boolean continuePlaying(boolean decision){ return decision; } } class AIplayer extends Player { private int ptsOnrow; public AIplayer(String name){ super(name); ptsOnrow = 0; } public boolean doLogic(int dice){ if(super.doLogic(dice)){ ptsOnrow+=dice; return true; } else { ptsOnrow = 0; return false; } } public boolean checkPtsOnRow(){ if(ptsOnrow>=20){ ptsOnrow =0; return false; } return true; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote