-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMatch.java
More file actions
185 lines (177 loc) · 7.36 KB
/
Copy pathMemoryMatch.java
File metadata and controls
185 lines (177 loc) · 7.36 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
import java.util.*;
public class MemoryMatch
{
public static void main(String[] args)
{
final int NUM_PAIRS = 4;
int board[] = new int[NUM_PAIRS*2+1]; // ignore the ZERO element spot
boolean printElement[] = new boolean[NUM_PAIRS*2+1]; // defaulted all to 'false'
int temp, position1, position2,i;
// fill board will fill with [1,1,2,2,3,3,4,4]
int k=1;
for (i=1; i<=NUM_PAIRS*2; i+=2)
{
board[i] = k;
board[i+1] = k;
k++;
}
// shuffle the board 1000 times
for (i=1; i<=1000; i++)
{ // pick 2 random positions to swap
position1 = (int) (Math.random() * (board.length-1) + 1);
position2 = (int) (Math.random() * (board.length-1) + 1);
temp = board[position1];
board[position1] = board[position2];
board[position2] = temp;
}
System.out.print("SHUFFLED: ");
for (i=1; i<board.length; i++)
{
System.out.print( " " + board[i] + " " );
}
System.out.println("\n\n");
System.out.print("printElement: ");
for (i=1; i<board.length; i++)
{
System.out.print( printElement[i] + ", " );
}
System.out.println("\n \n \n");
int P1score=0, P2score=0, count=0;
System.out.println("Player 1 name:");
Scanner console=new Scanner (System.in);
String P1=console.nextLine();
System.out.println("Player 2 name:");
String P2=console.nextLine();
while (count<4)
{
System.out.println(P1 + "'s 1st choice");
int P1one=console.nextInt();
printElement[P1one]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
System.out.println();
System.out.println(P1 + "'s 2nd choice");
int P1two=console.nextInt();
printElement[P1two]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
printElement[P1one]=false;
printElement[P1two]=false;
System.out.println();
if (board[P1one]!=board[P1two])
System.out.println("No match. " + P2 + "'s turn");
while (board[P1one]==board[P1two])
{
P1score++;
System.out.println("Match! " + P1 + " goes again\n");
System.out.println("Player 1: " + P1+ " Score: " + P1score);
System.out.println("Player 2: " + P2+ " Score: " + P2score +"\n");
count++;
P2=console.nextLine();
System.out.println(P1 + "'s 1st choice");
P1one=console.nextInt();
printElement[P1one]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
System.out.println();
System.out.println(P1 + "'s 2nd choice");
P1two=console.nextInt();
printElement[P1two]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
printElement[P1one]=false;
printElement[P1two]=false;
System.out.println();
if (board[P1one]!=board[P1two])
System.out.println("No match. " + P2 + "'s turn");
}
System.out.println(P2 + "'s 1st choice");
int P2one=console.nextInt();
printElement[P2one]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
System.out.println();
System.out.println(P2 + "'s 2nd choice");
int P2two=console.nextInt();
printElement[P2two]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
printElement[P2one]=false;
printElement[P2two]=false;
System.out.println();
if (board[P2one]!=board[P2two])
System.out.println("No match. " + P1 + "'s turn");
while (board[P2one]==board[P2two])
{
P2score++;
System.out.println("Match! " + P2 + " goes again\n");
System.out.println("Player 1: " + P1+ " Score: " + P1score);
System.out.println("Player 2: " + P2+ " Score: " + P2score + "\n");
count++;
P2=console.nextLine();
System.out.println(P2 + "'s 1st choice");
P2one=console.nextInt();
printElement[P2one]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
System.out.println();
System.out.println(P2 + "'s 2nd choice");
P2two=console.nextInt();
printElement[P2two]=true;
for (i=1; i<board.length; i++)// Print out 'board' via 'printElement'
{
if (printElement[i]==true) // || i==guess1 || i==guess2)
System.out.print(" " +board[i] + " ");
else
System.out.print(" * ");
}
printElement[P2one]=false;
printElement[P2two]=false;
System.out.println();
if (board[P2one]!=board[P2two])
System.out.println("No match. " + P1 + "'s turn");
}
}
if (P1score>P2score)
System.out.println(P1 + " wins!");
if (P2score>P1score)
System.out.println(P2 + " wins!");
else if(P1score==P2score)
System.out.println("Tie!");
}
}