-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixGame.java
More file actions
235 lines (204 loc) · 6.06 KB
/
MatrixGame.java
File metadata and controls
235 lines (204 loc) · 6.06 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//Name: Rishi Saravanan
//Date: 1/20/21
//Program Name: MatrixGame
//Program Goal: Use a 2d array to play a Matrix Game with 2 people
import java.util.Scanner;
public class MatrixGame {
int[][] array = new int[9][9];
int[] rowOrderedArray;
int[] colOrderedArray;
int[] row = new int[7];
int[] col = new int[7];
int playerOneScore = 0;
int playerTwoScore = 0;
int pla1col1 = 0;
int pla1col2 = 0;
int pla1row1 = 0;
int pla1row2 = 0;
int pla2col1 = 0;
int pla2col2 = 0;
int pla2row1 = 0;
int pla2row2 = 0;
Scanner s1 = new Scanner(System.in);
public static void main(String[] args) {
MatrixGame m1 = new MatrixGame();
m1.runner();
}
public void runner() {
System.out.print("\n\n\n");
fillMatrix (array);
calculateSum(array, row, col);
int[] colArray = copy(col);
markTopThreeRowsAndColumns(row, col, array);
printValues(array, colArray);
printPlayerScores();
do {
System.out.println("Player1(row), it's your turn:");
System.out.print("Column first then row: ");
pla1col1 = s1.nextInt();
pla1row1 = s1.nextInt();
pla1col2 = s1.nextInt();
pla1row2 = s1.nextInt();
boolean validRowCol = checkRowDistance(pla1col1, pla1col2, pla1row1, pla1row2);
if (validRowCol) {
int num = array[pla1row2-1][pla1col2-1];
array[pla1row2-1][pla1col2-1] = array[pla1row1-1][pla1col1-1];
array[pla1row1-1][pla1col1-1] = num;
calculateSum(array, row, col);
colArray = copy(col);
markTopThreeRowsAndColumns(row, col, array);
printValues(array, colArray);
printPlayerScores();
System.out.println("Player2(col), it's your turn:");
System.out.print("Column first then row: ");
pla2col1 = s1.nextInt();
pla2row1 = s1.nextInt();
pla2col2 = s1.nextInt();
pla2row2 = s1.nextInt();
validRowCol = checkColDistance(pla1col1, pla1col2, pla1row1, pla1row2);
if (validRowCol) {
int num2 = array[pla2row2-1][pla2col2-1];
array[pla2row2-1][pla2col2-1] = array[pla2row1-1][pla2col1-1];
array[pla2row1-1][pla2col1-1] = num2;
calculateSum(array, row, col);
colArray = copy(col);
markTopThreeRowsAndColumns(row, col, array);
printValues(array, colArray);
printPlayerScores();
}
} else {
System.out.print("\nThat's not a legal move. Try Again: ");
pla1col1 = s1.nextInt();
pla1col2 = s1.nextInt();
pla1row1 = s1.nextInt();
pla1row2 = s1.nextInt();
}
} while (pla1col1 != 0 && pla1col2 != 0 && pla1row1 != 0 && pla1row2 != 0 && pla2col1 != 0 && pla2col2 != 0 && pla2row1 != 0 && pla2row2 != 0);
if (playerOneScore > playerTwoScore ) {
System.out.println("Player 1 Wins!");
} else if(playerOneScore == playerTwoScore) {
System.out.println("Its a tie!");
} else {
System.out.println("Player 2 Wins!");
}
System.out.println("Thanks For Playing.");
System.out.print("\n\n");
}
public int[] maxSum(int[] array) {
for (int z = 0; z < 5; z++) {
for (int y = 0; y < array.length - 1; y++) {
int max = Math.max(array[y], array[y + 1]);
int min = Math.min(array[y], array[y + 1]);
array[y] = min;
array[y + 1] = max;
}
}
return array;
}
public int[] copy(int[] array) {
int[] retVal = new int[array.length];
for (int z = 0; z < array.length; z++) {
retVal[z] = array[z];
}
return retVal;
}
public void printValues(int[][] array, int[] colArray) {
String rowHeader = "| |";
for (int i = 1; i <= 7; i++) {
rowHeader+=i;
rowHeader+="|";
}
rowHeader+=" ";
System.out.println(rowHeader);
for (int a = 0; a < 7; a++) {
System.out.print("|"+ (a+1) + "|");
for (int b = 0; b < 7; b++) {
System.out.print(array[a][b] + " ");
}
if(array[a][7] == -1)
System.out.print("*");
System.out.println();
}
System.out.print(" ");
for (int a=0; a < array.length; a++) {
if(array[7][a] == -1)
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
public void printPlayerScores() {
System.out.println("Player 1 score = " + playerOneScore);
System.out.println("Player 2 score = " + playerTwoScore);
}
public void fillMatrix(int[][] array) {
for (int x = 0; x < 7; x++) {
for (int y = 0; y < 7; y++) {
int num = (int) (Math.random() * 10);
array[x][y] = num;
}
}
}
public void calculateSum(int[][] array, int[] row, int[] col) {
int rowSum = 0;
int colSum = 0;
for (int i = 0; i < 7; i++) {
rowSum = 0;
for (int j = 0; j < 7; j++) {
rowSum = rowSum + array[i][j];
}
row[i] = rowSum;
}
for (int i = 0; i < 7; i++) {
colSum = 0;
for (int j = 0; j < 7; j++) {
colSum = colSum + array[j][i];
}
col[i] = colSum;
}
for (int r = 0; r < 7; r++) {
array[r][7] = row[r];
}
for (int r = 0; r < 7; r++) {
array[7][r] = col[r];
}
}
public void markTopThreeRowsAndColumns(int[] row, int[] col, int[][]array) {
rowOrderedArray = maxSum(row);
colOrderedArray = maxSum(col);
playerOneScore = 0;
playerTwoScore = 0;
for(int c = 0; c < array.length; c++) {
if(array[c][7] == rowOrderedArray[5] || array[c][7] == rowOrderedArray[4] || array[c][7] == rowOrderedArray[6]) {
playerOneScore+=array[c][7];
array[c][7] = -1;
} else {
array[c][7] = 0;
}
}
for(int c = 0; c < array.length; c++) {
if(array[7][c] == colOrderedArray[5] || array[7][c] == colOrderedArray[4] || array[7][c] == colOrderedArray[6]) {
playerTwoScore+=array[7][c];
array[7][c] = -1;
} else {
array[7][c] = 0;
}
}
}
public boolean checkRowDistance(int pla1col1, int pla1col, int pla1row1, int pla1row2) {
int pla1ColDiff = Math.abs(pla1col1-pla1col2);
int pla1RowDiff = Math.abs(pla1row1-pla1row2);
if(pla1ColDiff == 0 || pla1RowDiff == 0 && (pla1ColDiff <= 1 || pla1RowDiff <= 1))
return true;
else
return false;
}
public boolean checkColDistance(int pla1col1, int pla1col, int pla1row1, int pla1row2) {
int pla2ColDiff = Math.abs(pla2col1-pla2col2);
int pla2RowDiff = Math.abs(pla2row1-pla2row2);
if(pla2ColDiff == 0 || pla2RowDiff == 0 && (pla2ColDiff <= 1 || pla2RowDiff <= 1))
return true;
else
return false;
}
}