-
Notifications
You must be signed in to change notification settings - Fork 0
/
humanPlayer.js
149 lines (126 loc) · 3.47 KB
/
humanPlayer.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const readline = require('readline')
//implement game called Battleship
//User plays game w/ terminal
//State of game represented by 10x10 Grid Board
//You can make them flexible.
//Square attack
//Ship?
//Hit
//No ship?
//Miss
//Displayed
//Hits
//Misses
//Unattacked Squares
//Attacked ships
//Not displayed
//Unattacked ships
class HumanPlayer {
constructor() {
rl = readline.createInterface(process.stdin, process.stdout);
}
};
//Grid:
//i = row
//j = column
class Board {
constructor (numRows, numCols, numShips, player) {
this.numRows = numRows; //10
this.numCols = numCols; //10
this.numShips = numShips;
this.player = player;
}
createGrid() {
//"this" makes a variable usable throughout an object.
this.grid = [];
for (let row = 1; row <= this.numRows; row++) {
let xAxis = [];
for (let column = 1; column <= this.numCols; column++) {
if (this.numShips > 0) {
this.numShips--;
xAxis.push("s");
} else {
xAxis.push(null);
}
}
this.grid.push(xAxis);
}
}
gridDisplay() {
return this.grid;
}
shipsRemaining() {
console.log(`${this.numShips} are remaining`)
}
gameStatus() {
if(this.numShips === 0) {
console.log(`${this.numShips} remaining, game over!`)
}
}
// if the space has been called upon before is invalid
// if the space does not exist it is invalid
inValidMove() {
console.log("This move is invalid!")
}
playerInput(coordinates) {
let destroyed = 'x';
let miss = 'm';
for (let outer = 0; outer < this.grid.length; outer++) {
let subGrid = this.grid[outer];
for (let inner = 0; inner < subGrid.length; inner++) {
let space = subGrid[inner];
if (space === 's') {
space = destroyed;
this.numShips--;
} else {
space = miss;
}
}
}
this.gridDisplay();
this.shipsRemaining();
this.gameStatus();
}
grabCoordinates(r, c) {
console.log("Make your move!")
// rl.question('Enter row', (r) => {
// cb(answer);
// rl.close();
// });
let coordinates = [];
for (let row = 1; row <= this.numRows; row++) {
if (r === row) {
coordinates.push(row);
} else {
this.inValidMove();
}
};
for (let column = 1; column <= this.numCols; column++) {
if (c === column) {
coordinates.push(column)
} else {
this.inValidMove();
}
}
playerInput(coordinates);
}
}
//const player1 = new HumanPlayer
const boardGame = new Board(3, 3, 1);
boardGame.createGrid()
boardGame.playerInput([1, 2]);
//CB
//Next step after user input is received.
//Next square player wants to attack.
//1st. Player makes move.
//2nd. Hit/Miss?
//3rd. If hit, go again.
//3.1 If miss, it's now opponent's turn
// HP:
// Ship hitpoints (Denoted by counter)
// counter = however many grid spaces it occupies
// if hit, counter -= 1;
// if counter === 0, ship is DESTROYED!!!
//Ship Placement:
//
module.exports = HumanPlayer