-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello.h
84 lines (76 loc) · 2.25 KB
/
othello.h
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
//
// Created by Sun Yaozhu on 2018-06-06
// Copyright (c) 2018 Sun Yaozhu. All rights reserved.
//
#ifndef OTHELLO_H_
#define OTHELLO_H_
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <string>
#include <utility>
class OthelloBoard {
public:
enum Player { kBlack = 0, kWhite = 1, kUnknown = 2 };
OthelloBoard();
OthelloBoard(std::string board, Player player);
Player WhoseTurn() const;
bool CanPlay(int row, int col) const;
bool CanPlay() const;
bool IsOver() const;
OthelloBoard Play(int row, int col) const;
OthelloBoard Pass() const;
int AbsoluteScore() const;
int WeightedScore() const;
int Negamax(int depth, int alpha, int beta) const;
std::pair<int, int> LastMove() const;
void PrintBoard(std::ostream& out) const;
void PrintResult(std::ostream& out) const;
class Iterator : public std::iterator<std::input_iterator_tag, OthelloBoard> {
public:
explicit Iterator(const OthelloBoard* othello)
: othello_(othello), moves_(othello->moves_) {}
Iterator(const OthelloBoard* othello, std::uint64_t moves)
: othello_(othello), moves_(moves) {}
Iterator& operator++() {
// Find first set and then minus it
moves_ -= moves_ & -moves_;
return *this;
}
bool operator!=(Iterator other) const {
return othello_ != other.othello_ || moves_ != other.moves_;
}
OthelloBoard operator*() const {
// Can be replaced by `__builtin_ffs` in GCC
assert(moves_);
auto index = 0;
std::uint64_t mask = 1;
while ((moves_ & mask) == 0) {
++index;
mask <<= 1;
}
return othello_->Play(index / 8, index % 8);
}
private:
const OthelloBoard* othello_;
std::uint64_t moves_;
};
Iterator begin() const { return Iterator(this); }
Iterator end() const { return Iterator(this, 0); }
protected:
bool IsInside(int row, int col) const;
Player Get(int row, int col) const;
void Set(int row, int col, Player player);
std::uint64_t Shift(std::uint64_t board, int dir) const;
void GenerateMoves();
char Repr(int row, int col) const;
private:
std::uint64_t board_[2];
std::uint64_t moves_;
Player self_;
Player oppo_;
int last_row_ = -1;
int last_col_ = -1;
};
#endif // OTHELLO_H_