forked from coding-horror/basic-computer-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChomp.java
More file actions
157 lines (148 loc) · 4.8 KB
/
Copy pathChomp.java
File metadata and controls
157 lines (148 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.Scanner;
public class Chomp{
int rows;
int cols;
int numberOfPlayers;
int []board;
Scanner scanner;
Chomp(){
System.out.println("\t\t\t\tCHOMP");
System.out.println("\t\tCREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
System.out.println("THIS IS THE GAME OF CHOMP (SCIENTIFIC AMERICAN, JAN 1973)");
System.out.print("Do you want the rules (1=Yes, 0=No!) ");
scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if(choice != 0){
System.out.println("Chomp is for 1 or more players (Humans only).\n");
System.out.println("Here's how a board looks (This one is 5 by 7):");
System.out.println("\t1 2 3 4 5 6 7");
System.out.println(" 1 P * * * * * *\n 2 * * * * * * *\n 3 * * * * * * *\n 4 * * * * * * *\n 5 * * * * * * *");
System.out.println("\nThe board is a big cookie - R rows high and C columns \nwide. You input R and C at the start. In the upper left\ncorner of the cookie is a poison square (P). The one who\nchomps the poison square loses. To take a chomp, type the\nrow and column of one of the squares on the cookie.\nAll of the squares below and to the right of that square\n(Including that square, too) disappear -- CHOMP!!\nNo fair chomping squares that have already been chomped,\nor that are outside the original dimensions of the cookie.\n");
System.out.println("Here we go...\n");
}
startGame();
}
private void startGame(){
System.out.print("How many players ");
numberOfPlayers = scanner.nextInt();
while(numberOfPlayers < 2){
System.out.print("How many players ");
numberOfPlayers = scanner.nextInt();
}
System.out.print("How many rows ");
rows = scanner.nextInt();
while(rows<=0 || rows >9){
if(rows <= 0){
System.out.println("Minimun 1 row is required !!");
}
else{
System.out.println("Too many rows(9 is maximum). ");
}
System.out.print("How many rows ");
rows = scanner.nextInt();
}
System.out.print("How many columns ");
cols = scanner.nextInt();
while(cols<=0 || cols >9){
if(cols <= 0){
System.out.println("Minimun 1 column is required !!");
}
else{
System.out.println("Too many columns(9 is maximum). ");
}
System.out.print("How many columns ");
cols = scanner.nextInt();
}
board = new int[rows];
for(int i=0;i<rows;i++){
board[i]=cols;
}
printBoard();
scanner.nextLine();
move(0);
}
private void printBoard(){
System.out.print(" ");
for(int i=0;i<cols;i++){
System.out.print(i+1);
System.out.print(" ");
}
for(int i=0;i<rows;i++){
System.out.print("\n ");
System.out.print(i+1);
System.out.print(" ");
for(int j=0;j<board[i];j++){
if(i == 0 && j == 0){
System.out.print("P ");
}
else{
System.out.print("* ");
}
}
}
System.out.println("");
}
private void move(int player){
System.out.println(String.format("Player %d",(player+1)));
String input;
String [] coordinates;
int x=-1,y=-1;
while(true){
try{
System.out.print("Coordinates of chomp (Row, Column) ");
input = scanner.nextLine();
coordinates = input.split(",");
x = Integer.parseInt(coordinates[0]);
y = Integer.parseInt(coordinates[1]);
break;
}
catch(Exception e){
System.out.println("Please enter valid coordinates.");
continue;
}
}
while(x>rows || x <1 || y>cols || y<1 || board[x-1]<y){
System.out.println("No fair. You're trying to chomp on empty space!");
while(true){
try{
System.out.print("Coordinates of chomp (Row, Column) ");
input = scanner.nextLine();
coordinates = input.split(",");
x = Integer.parseInt(coordinates[0]);
y = Integer.parseInt(coordinates[1]);
break;
}
catch(Exception e){
System.out.println("Please enter valid coordinates.");
continue;
}
}
}
if(x == 1 && y == 1){
System.out.println("You lose player "+(player+1));
int choice = -1 ;
while(choice != 0 && choice != 1){
System.out.print("Again (1=Yes, 0=No!) ");
choice = scanner.nextInt();
}
if(choice == 1){
startGame();
}
else{
System.exit(0);
}
}
else{
for(int i=x-1;i<rows;i++){
if(board[i] >= y){
board[i] = y-1;
}
}
printBoard();
move((player+1)%numberOfPlayers);
}
}
public static void main(String []args){
new Chomp();
}
}