In: Computer Science
Java Proect
Project Outcomes
Develop a Java program that:
•
creates a user designed class
•
uses proper design techniques including reading UML Class
Diagrams
•
reads input from the keyboard using a Scanner Object and its
methods
•
uses selection (if and if else) statements
•
uses iteration (while, do while or for) statements
•
uses String comparison methods.
•
follows standard acceptable programming practices.
•
handles integer overflow errors
Prep Readings:
Zybooks chapter 1 - 6
1. General
The game of Stix is a simplified version of an ancient game and somehow
looks like this:
●
It is played by two players
●
A number of sticks (like matches) are placed on a table.
●
The first player takes 1, 2 or 3 sticks away, provided that there that
many on the table.
●
Then the second player takes 1, 2 or 3 sticks away (if possible), and so
on.
●
Whoever takes the last stick, loses (!)
Your program does the following:
1.
It asks the initial number or stix (between 5 and 30)
2.
It asks if the computer (program) takes first
3.
It then lets the user and the computer take turns until the number of
stix drops to 0.
4.
Depending on who took the last stix, it declares the winner.
A simple run looks like this (Computer goes first and wins)
How many stix to begin with? [5 - 30] > 12
Computer goes first? [y/n] > y
Stix on the table: ||||||||||||
Computer takes 3
Stix on the table: |||||||||
How many stix to take? [1 - 3] > 2
Stix on the table: |||||||
Computer takes 2
Stix on the table: |||||
How many stix to take? [1 - 3] > 1
Stix on the table: ||||
Computer takes 3
Stix on the table: |
How many stix to take? [1 - 1] > 1
Stix on the table:
I win!
Here is another short run (Player goes first and wins)
How many stix to begin with? [5 - 30] > 14
Computer goes first? [y/n] > n
Stix on the table: ||||||||||||||
How many stix to take? [1 - 3] > 1
Stix on the table: |||||||||||||
Computer takes 1
Stix on the table: ||||||||||||
How many stix to take? [1 - 3] > 3
Stix on the table: |||||||||
Computer takes 1
Stix on the table: ||||||||
How many stix to take? [1 - 3] > 3
Stix on the table: |||||
Computer takes 1
Stix on the table: ||||
How many stix to take? [1 - 3] > 3
Stix on the table: |
Computer takes 1
Stix on the table:
You win!
There are of course two more possible situations: a) Computer goes first,
Player wins and b) Player goes first, Computer wins. Actually, all four can
happen. So be prepared for that!
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! ===========================================================================
import java.util.Random; import java.util.Scanner; public class Stix { private static Scanner scanner = new Scanner(System.in); private static Random random = new Random(); public static void main(String[] args) { int count = getStickCount(); boolean computerFirst = computerGoesFirst(); displaySticks(count); if (computerFirst) { int computer = getComputerCount(count); System.out.println("Computer takes " + computer); count -= computer; displaySticks(count); } while (true) { int player = getUserCount(count); displaySticks(count-player); if (count == 1 && player==1) { System.out.println("I win!"); break; } count -= player; int computer = getComputerCount(count); System.out.println("Computer takes " + computer); displaySticks(count-computer); if(count == 1 && computer==1){ System.out.println("You win!"); break; } count -= computer; } } private static int getUserCount(int currentStix) { int count; while (true) { System.out.printf("How many stick to take? [1 - 3] > "); count = scanner.nextInt(); if (1 <= count && count <= 3 && count <= currentStix) { return count; } } } private static int getStickCount() { int count = 0; while (true) { System.out.print("How many stix to begin with?[5-30] >"); count = scanner.nextInt(); scanner.nextLine(); if (5 <= count && count <= 30) return count; } } private static void displaySticks(int count) { System.out.print("Stix on the table "); for (int i = 1; i <= count; i++) System.out.printf("|"); System.out.println(); } private static boolean computerGoesFirst() { char letter; while (true) { System.out.print("Computer goes first? [y/n] > "); letter = scanner.nextLine().charAt(0); if (letter == 'y' || letter == 'Y') return true; else if (letter == 'n' || letter == 'N') return false; else continue; } } private static int getComputerCount(int count) { if (count > 3) return 1 + random.nextInt(3); else if (count == 3) return 2; else if (count == 2) return 1; else return 1; } }