-
Notifications
You must be signed in to change notification settings - Fork 12
/
nim.cpp
86 lines (74 loc) · 2.32 KB
/
nim.cpp
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
//
// Nim AI (three moutaions)
// win pattern: xor = 0
//
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
// 3 山の石の個数を入力
int A, B, C;
cout << "The number of the first pile: ";
cin >> A;
cout << "The number of the second pile: ";
cin >> B;
cout << "The number of the third pile: ";
cin >> C;
// 先手と後手の戦略
string ans;
cout << "Are you first? (yes / no): " << endl;
cin >> ans;
bool yours = (ans == "yes" ? true : false);
// 石を取れなくなるまでプレイする
while (A + B + C > 0) {
cout << "-----------------" << endl;
// プレイヤーのターン (例外処理は省略)
if (yours) {
// 石の山と取る石の個数を入力
int which, num;
cout << "Your Turn" << endl;
cout << "Current state: ("
<< A << ", "
<< B << ", "
<< C << ")" << endl;
cout << "Which pile? (1 or 2 or 3): ";
cin >> which;
cout << "The number of stones? : ";
cin >> num;
// 石の個数を変更
if (which == 1) A -= num;
else if (which == 2) B -= num;
else C -= num;
}
// AI のターン
else {
cout << "AI's turn" << endl;
cout << "Current state: ("
<< A << ", "
<< B << ", "
<< C << ")" << endl;
// A, B, C の XOR 和を S とする
int S = A ^ B ^ C;
// S > 0 のときは勝ちパターンにする
if (S > 0) {
if ((A ^ S) < A) A ^= S;
else if ((B ^ S) < B) B ^= S;
else C ^= S;
}
// それ以外の場合はランダムに石を減らす
else {
if (A > 0) A = rand() % A;
else if (B > 0) B = rand() % B;
else C = rand() % C;
}
cout << "After AI's turn: ("
<< A << ", "
<< B << ", "
<< C << ")" << endl;
}
// 手番を入れ替え
yours = !yours;
}
if (yours) cout << "You lose..." << endl;
else cout << "You win!" << endl;
}