-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
62 lines (56 loc) · 1.45 KB
/
game.cpp
File metadata and controls
62 lines (56 loc) · 1.45 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
#include "game.h"
#include <iostream>
#include <vector>
using namespace std;
const int p[9] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
char board[11] = {'-', '-', '-', '\n', '-', '-', '-', '\n', '-', '-', '-'};
vector<int> devide(long long n) {
vector<int> power(9, 0);
for (int i = 0; i < 9; i++) {
while (n % p[i] == 0) {
power[i]++;
n /= p[i];
}
}
return power;
}
long long play(long long code, int player, int x, int y) {
int rank = (x-1)*3+(y-1);
return code + (p[rank] ^ player);
}
void print(long long code) {
vector<int> power = devide(code);
for (int i = 0; i < 9; i++) {
if (power[i] == 1)
board[i] = 'x';
else if (power[i] == 2)
board[i] = 'o';
else
board[i] = '-';
}
for (int i = 0; i < 11; i++)
{
if (i % 4 != 3)
cout << board[i] << " ";
else
cout << board[i];
}
}
int check_winner(long long code) {
for (int i = 0; i < 3; i++) {
if (board[i] != '0' && board[i] == board[i + 3] && board[i] == board[i + 6]) {
return (board[i] == 'x') ? 1 : 2;
}
}
for (int i = 0; i < 9; i += 3) {
if (board[i] != '-' && board[i] == board[i + 1] && board[i] == board[i + 2]) {
return (board[i] == 'x') ? 1 : 2;
}
}
if (board[0] != '-' && board[0] == board[4] && board[0] == board[8]) {
return (board[0] == 'x') ? 1 : 2;
} else if (board[2] != '-' && board[2] == board[4] && board[2] == board[6]) {
return (board[2] == 'x') ? 1 : 2;
}
return 0;
}