-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.hpp
More file actions
90 lines (72 loc) · 2.5 KB
/
Game.hpp
File metadata and controls
90 lines (72 loc) · 2.5 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
#ifndef GAME_HPP
#define GAME_HPP
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "Board.hpp"
#include "Player.hpp"
class Game{
public:
Game() {
player1 = new Player("Player 1", true, true, false); // name, is_human, is_white, on_top
player2 = new Player("Player 2 (AI)", false, false, true);
this->current_player = player1;
board.setFenPosition("4k3/6N1/5b2/3R4/8/8/8/4K3 b - - 0 3");
board.init_board();
}
~Game() {
delete player1;
delete player2;
}
void setRenderer(SDL_Renderer* renderer){
board.setRenderer(renderer);
}
void render(){
board.render();
}
void start_dragging(SDL_Point mouse_pos){
if (current_player->isHuman()){
//Get Row and col of "drag from" position
Position move_from_pos = board.getTilePos(mouse_pos);
current_player->setMoveFrom(move_from_pos);
}
}
void update_mouse_pos(SDL_Point mouse_pos){
board.updateMouse(mouse_pos);
}
void end_dragging(SDL_Point mouse_pos){
if (current_player->isHuman()){
//Get Row and col of "drag from" position
Position move_to_pos = board.getTilePos(mouse_pos);
current_player->setMoveTo(move_to_pos);
//Make the human move;
Move* human_move = current_player->performMove(board);
if (human_move){ // If the move is valid;
board.makeMove(human_move);
switch_player();
}
board.stopDragging();
}
}
void update(){
if (!current_player->isHuman()){ //If it is an AI
//Make the AI move;
Move* ai_move = current_player->performMove(board);
if (ai_move){ // If the move is valid;
board.makeMove(ai_move);
switch_player();
}else{
cout << "There are no legal moves!!" << endl;
}
}
}
private:
void switch_player(){
current_player = (current_player == player1) ? player2 : player1;
cout << "It " << current_player->getName() << "\'s turn\n";
}
Board board;
Player* player1;
Player* player2;
Player* current_player;
};
#endif