-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.h
133 lines (125 loc) · 2.05 KB
/
player.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
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
128
129
130
131
132
133
#ifndef _PLAYER_H_
#define _PLAYER_H_
#include "X.h"
#include "O.h"
class Player {
public:
Player(WINDOW * window, int y, int x);
void moveUp();
void moveDown();
void moveRight();
void moveLeft();
int getMove();
void markPosition();
void display();
int getBoardY();
int getBoardX();
char getPiece();
private:
int y, x, yMax, xMax, boardY, boardX, turn;
char currentPiece;
WINDOW * currentWindow;
};
Player::Player(WINDOW * window, int y, int x) {
currentWindow = window;
Player::y = y;
Player::x = x;
Player::boardY = 0;
Player::boardX = 0;
Player::turn = 1;
getmaxyx(currentWindow, yMax, xMax);
keypad(currentWindow, true);
}
/*int Player::getWindowX() {
return x;
}
int Player::getWindowY() {
return y;
}*/
int Player::getBoardX() {
return boardX;
}
int Player::getBoardY() {
return boardY;
}
char Player::getPiece() {
return currentPiece;
}
void Player::markPosition() {
if (turn == 1) {
X *xpiece = new X(currentWindow);
xpiece->mark(y, x);
currentPiece = 'x';
turn++;
} else if (turn == 2) {
O *opiece = new O(currentWindow);
opiece->mark(y, x);
currentPiece = 'o';
turn--;
}
refresh();
wrefresh(currentWindow);
}
void Player::moveRight() {
boardX++;
x = x + 8;
if (x > (xMax / 2) + 4) {
boardX = 2;
x = xMax - 5;
}
}
void Player::moveLeft() {
boardX--;
x = x - 8;
if (x < (xMax / 2)) {
boardX = 0;
x = 4;
}
}
void Player::moveUp() {
boardY--;
y = y - 4;
if (y < (yMax / 2)) {
boardY = 0;
y = 2;
}
}
void Player::moveDown() {
boardY++;
y = y + 4;
if (y > (yMax / 2)) {
boardY = 2;
y = yMax - 3;
}
}
int Player::getMove() {
int choice = wgetch(currentWindow);
switch(choice) {
case KEY_RIGHT:
moveRight();
currentPiece = '@';
break;
case KEY_LEFT:
moveLeft();
currentPiece = '@';
break;
case KEY_UP:
moveUp();
currentPiece = '@';
break;
case KEY_DOWN:
moveDown();
currentPiece = '@';
break;
case '\n':
markPosition();
break;
default:
break;
}
return choice;
}
void Player::display() {
wmove(currentWindow, y, x);
}
#endif