-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
67 lines (60 loc) · 1.61 KB
/
Board.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
#include "Board.h"
#include <iostream>
#include <bitset>
using std::cout;
using std::endl;
using std::bitset;
Board::Board():
board(0),
bottommask(0x01010101010101),
moves(0),
playedMask(0){
}
void Board::Print(bool complete) const{
//prints the board as if it were rotated 90 degrees
for(int i = 0; i<7; i++){
cout<<static_cast<bitset<8>>((board>>(i*8))&columnMask)<<endl;
}
//print the played moves so the user know if a 0 is because
//there is no move or player 0 has a piece there
if(complete){
cout<<endl;
for(int i = 0; i<7; i++){
cout<<static_cast<bitset<8>>((playedMask>>(i*8))&columnMask)<<endl;
}
}
}
bool Board::PlayMove(ushort column){
bool playable = ((playedMask>>(column*8))&columnMask)<0x3f;
//player represented with 1 plays
if(playable){
//Adds a piece
playedMask |= bottomsinglemask[column]+playedMask;
//Flips all played positions, including the most recent
//which was a 0 because there was nothing there and now it's
//1 since player 1 played there
board ^= playedMask;
moves++;
}
return playable;
}
bool Board::Winn()const {
//horizontal win
uint64_t aux = board&(board>>8);
if((aux&(aux>>16))>0)
return true;
//diagonal 1 win
aux = board&(board>>9);
if((aux&(aux>>18))>0)
return true;
//diagonal 2 win
aux = board&(board>>7);
if((aux&(aux>>14))>0)
return true;
//vertical win
aux = board&(board>>1);
if((aux&(aux>>2))>0)
return true;
//draw
return false;
}