-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.java
More file actions
160 lines (130 loc) · 5.7 KB
/
fib.java
File metadata and controls
160 lines (130 loc) · 5.7 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
158
159
160
import java.util.ArrayList;
import java.util.Scanner;
public class fib {
public static void main(String[] args) {
Scanner mainScanner = new Scanner(System.in);
ArrayList<Integer> heapArray = new ArrayList<>();
//Creation of all the necessary sentences and integers
//created in final variables and normal variables
final String CHOOSE_HEAP ="Choose a heap (1-";
final String NUMBER_OF_TOKENS ="The number of tokens you may take is between 1 and ";
final String TOKENS_TO_BE_TAKEN = "How many tokens do you want to take? ";
final String ERROR_CATCHING_IN_THE_TERMINAL = "Incorrect value entered in the terminal = ";
final String ERROR_CHECKING_USER_ENTERS_RIGHT_HEAP = "Enter a valid heap number between 1 and ";
final String EMPTY_HEAP = "There are no tokens left inside ";
final String ERROR_INCORRECT_HEAP_CHOOSEN = "You must enter in a integer between 1 and ";
final int DEFUALT_HEAP_SIZE = 9;
final int DEFUALT_NUMBER_OF_HEAPS = 3;
int roundCounter = 0;
int maximumTokenToSelect =2;
//This will check if the user has enter in new heap sizes in the terminal
if (args.length>0) {
for (int i = 0; i < args.length; i++) {
//This will catch the users error(s) from the terminal and tell them that
//they have entered incorrect values and discard it
try {
heapArray.add(Integer.parseInt(args[i]));
}catch (Exception ex) {
System.out.println(ERROR_CATCHING_IN_THE_TERMINAL + args[i]);
}
}
}
// Initialize's default values in case error
if ((args.length == 0) || (heapArray.size() == 0)){
for (int k = 0; k < DEFUALT_NUMBER_OF_HEAPS; k++) {
heapArray.add(DEFUALT_HEAP_SIZE);
}
}
//This is the start of the game
while(!endGame(heapArray)) {
//output array
for (int j = 0; j < heapArray.size(); j++) {
System.out.println("Heap " + (j+1) + " " + heapArray.get(j));
}
//check whos playing
if(roundCounter %2 ==0) {
System.out.println("Player 1's turn.");
} else {
System.out.println("Player 2's turn.");
}
System.out.print(CHOOSE_HEAP + heapArray.size() + "): ");
//User Input validation
while(!mainScanner.hasNextInt()){
System.out.print(ERROR_CHECKING_USER_ENTERS_RIGHT_HEAP + heapArray.size() + ": ");
mainScanner.next();
}
int choosenheap = mainScanner.nextInt()-1;
if (choosenheap >= 0 && choosenheap <= (heapArray.size()-1)) {
if (heapArray.get(choosenheap) == 0 ) {
System.out.println(EMPTY_HEAP);
// handles possible range of values user can choose and subtracts from chosen heap
} else {
int tokens;
int numberOfTokemsInHeap;
// checks within range
if (maximumTokenToSelect > heapArray.get(choosenheap)) {
System.out.println(NUMBER_OF_TOKENS + heapArray.get(choosenheap));
System.out.print(TOKENS_TO_BE_TAKEN);
tokens = tokenErrorHandler(mainScanner ,heapArray.get(choosenheap));
numberOfTokemsInHeap = heapArray.get(choosenheap);
} else {
System.out.println(NUMBER_OF_TOKENS + maximumTokenToSelect);
System.out.print(TOKENS_TO_BE_TAKEN);
tokens = tokenErrorHandler(mainScanner ,maximumTokenToSelect);
numberOfTokemsInHeap = heapArray.get(choosenheap);
// increments maximum number for the next user
if ( maximumTokenToSelect == tokens ) {
maximumTokenToSelect= maximumTokenToSelect + tokens;
}
}
//subtractions from heap
numberOfTokemsInHeap = numberOfTokemsInHeap - tokens;
heapArray.set(choosenheap, numberOfTokemsInHeap);
roundCounter++;
}
} else {
System.out.println(ERROR_INCORRECT_HEAP_CHOOSEN + heapArray.size());
}
}
// displays who wins roundcounter changed
if(roundCounter %2 ==0) {
System.out.println("Player 2 wins");
} else {
System.out.println("Player 1 wins");
}
}
//method for User Input validation
private static int tokenErrorHandler(Scanner mainScanner, int size){
final String VALID_INTEGER = "Enter a valid token number: ";
final String ERROR_CHECKING_WITHIN_RANGE = "Enter the number of tokens between 1 and ";
boolean maincheck = true;
int checktokens =0;
//This verifies that the user enter's in the correct
//token value within the correct range
while(maincheck){
while(!mainScanner.hasNextInt()){
System.out.print(VALID_INTEGER);
mainScanner.next();
}
checktokens = (int)mainScanner.nextInt();
if(checktokens>=1 && checktokens<=size){
maincheck = false;
return checktokens;
}else{
System.out.print(ERROR_CHECKING_WITHIN_RANGE+size + ": ");
}
}
// in case error
return -1;
}
// method to check if thew game should end
private static boolean endGame(ArrayList<Integer> heapArray) {
int firsttokensberInArray= 0;
for (int j = 0; j < heapArray.size(); j++) {
if (heapArray.get(j) != firsttokensberInArray){
return false;
}
}
return true;
}
}