-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
127 lines (117 loc) · 4.62 KB
/
Board.cpp
File metadata and controls
127 lines (117 loc) · 4.62 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Created by vb-jakub on 19.05.24.
//
#include "Board.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
Board::Board(SnakeModel & snakeRef, int height, int width) : snake(snakeRef){
this->height=height;
this->width=width;
board.resize(width*height,{false,false});
gamestate=RUNNING;
isFood=false;
switch (snake.getGameMode()){
case EASY: setFood(); break;
case NORMAL: setNormalMap(); setFood(); break;
case HARD: setHardMap(); setFood(); break;
case DEBUG: setDebugMap(); break;
}
}
void Board::setNormalMap() {
for(int row=3; row<height-3; ++row) {
board[row*width+width/2-1].hasWall=true;
board[row*width+width/2].hasWall=true;
}
for(int col=3; col<width-3; ++col) {
board[(height/2-1)*width+col].hasWall=true;
board[(height/2)*width+col].hasWall=true;
}
}
void Board::setHardMap() {
for(int col=4; col<width-4; ++col) {
board[3*width+col].hasWall=true;
board[4*width+col].hasWall=true;
board[(height-5)*width+col].hasWall=true;
board[(height-4)*width+col].hasWall=true;
}
}
void Board::setDebugMap() {
isFood=true;
board[width].hasFood=true;
for(int colNr=0; colNr<width; ++colNr)
board[2*width+colNr].hasWall=true;
}
void Board::setFood() {
while(!isFood){
int randRow=rand() %height;
int randCol=rand() %width;
if(!board[randRow*width+randCol].hasWall and snake.getSnakeInfo(randRow,randCol)==' '){
board[randRow*width+randCol].hasFood=true;
isFood=true;
}
}
}
void Board::update() {
if(updateGameState()==RUNNING) {
snake.moveSnake();
std::pair<int, int> headPosition=snake.getHeadPosition();
if(isOnBoard(headPosition.first,headPosition.second) and board[headPosition.first*width+headPosition.second].hasFood){
snake.reduceSnakeTail(true);
board[headPosition.first*width+headPosition.second].hasFood=false;
isFood=false;
setFood();
}else snake.reduceSnakeTail(false);
if(!isOnBoard(headPosition.first, headPosition.second) or board[headPosition.first*width+headPosition.second].hasWall)
gamestate=FINISHED_LOSS;
if(snake.getSnakeInfo(headPosition.first, headPosition.second)=='x')
gamestate=FINISHED_LOSS;
}
}
// RUNNING - the game isn't yet finished
// FINISHED_WIN - snake is on all fields on board without fields with walls
// FINISHED_LOSS - snake hit a wall, snake go outside the board or snake bite itself
GameState Board::updateGameState() const {
if(gamestate==FINISHED_LOSS) return FINISHED_LOSS;
auto fieldsAmount=std::count_if(board.begin(),board.end(),[](Field board) {return !board.hasWall;});
if(fieldsAmount==snake.getSnakeLength()) return FINISHED_WIN;
return RUNNING;
}
//if the field is outside board - return '#' character
//if the field has wall - return 'W' character
//if the field has food - return 'F' character
//if the field hasn't wall or food - return '_' character
char Board::getFieldInfo(int row, int col) const {
if(!isOnBoard(row,col)) return '#';
if(board[row*width+col].hasWall) return 'W';
if(board[row*width+col].hasFood) return 'F';
return '_';
}
//display board as a string of 4 characters in square brackets:
//-first character inform us that the field has wall, if it is, will display 'W'
//-second character inform us that the field has food, if it is, will display 'F'
//-third character inform us that on the field is snake head, if it is, will display 'H'
//-fourth character inform us that on the field is snake tail, if it is, will display 'T'
//display also numbers of cols and rows
void Board::debug_display() const {
std::cout << " ";
for(int colNr=0; colNr<width; ++colNr)
std::cout << " " << std::setw(2) << colNr;
std::cout << std::endl;
for(int rowNr=0; rowNr<height; ++rowNr) {
std::cout << std::setw(2) << std::right << rowNr <<" ";
for(int colNr=0; colNr<width; ++colNr) {
std::cout << "[";
if(board[rowNr*width+colNr].hasWall) std::cout << 'W';
else std::cout << ".";
if(board[rowNr*width+colNr].hasFood) std::cout << 'F';
else std::cout << ".";
if(snake.getSnakeInfo(rowNr,colNr)=='H') std::cout << 'H';
else std::cout << ".";
if(snake.getSnakeInfo(rowNr,colNr)=='B' or snake.getSnakeInfo(rowNr,colNr)=='T') std::cout << 'T';
else std::cout << ".";
std::cout << "]";
}
std::cout << std::endl;
}
}