Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit a32dea9

Browse files
author
njacobs5074
committed
Fixed issue #1
1 parent 2e547ae commit a32dea9

File tree

2 files changed

+132
-69
lines changed

2 files changed

+132
-69
lines changed

app/computer.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ComputerPlayer.prototype.initStrategies = function () {
3131
this.strategies[8] = new EmptySideStrategy(this.board, this.me.letter);
3232
};
3333

34-
ComputerPlayer.prototype.addListeners = function() {
34+
ComputerPlayer.prototype.addListeners = function () {
3535
this.game.addListener(ComputerPlayer.gameListener, this);
3636
this.board.addListener(ComputerPlayer.boardListener, this);
3737
};
@@ -85,7 +85,7 @@ ComputerPlayer.findTwoInARow = function (board, letter) {
8585
return new TwoInARow(i, 1, TwoInARow.HORIZONTAL, i, 0);
8686
}
8787
else if (board.getTile(i, 0).player == letter && board.getTile(i, 2).player == letter && board.getTile(i, 1).unset()) {
88-
return new TwoInARow(i, 0, TwoInARow.HORIZONTAL, i,1);
88+
return new TwoInARow(i, 0, TwoInARow.HORIZONTAL, i, 1);
8989
}
9090
}
9191

@@ -202,6 +202,11 @@ function TwoInARow(row, col, orientation, unsetRow, unsetCol) {
202202
this.unsetCol = unsetCol;
203203
}
204204

205+
TwoInARow.prototype.toString = function () {
206+
return "{row=" + this.row + ", col=" + this.col + ", orientation=" + this.orientation +
207+
", unsetRow=" + this.unsetRow + ", unsetCol=" + this.unsetCol + "}";
208+
};
209+
205210
TwoInARow.HORIZONTAL = 0;
206211
TwoInARow.VERTICAL = 1;
207212
TwoInARow.DIAGONAL = 2;
@@ -221,7 +226,7 @@ function TurnOneStrategy(game, letter) {
221226
this.letter = letter;
222227
}
223228

224-
TurnOneStrategy.prototype.play = function() {
229+
TurnOneStrategy.prototype.play = function () {
225230
console.log("TurnOneStrategy: Turn = " + this.game.turn);
226231

227232
if (this.game.turn == 1 && this.letter == 'O' && this.game.board.getTile(1, 1).unset()) {
@@ -307,7 +312,7 @@ function BlockForkStrategy(board, opponentLetter, myLetter) {
307312
BlockForkStrategy.prototype.play = function () {
308313
console.log("BlockForkStrategy: Looking for opponent fork: " + this.opponentLetter);
309314

310-
var copyOfBoard = null, i, j, opponentForkStrategy;
315+
var copyOfBoard = null, i, j, opponentForkStrategy, forkedTile;
311316

312317
// Look for a place to put our letter such that it does not open up an opponent fork.
313318
for (i = 0; i < 3; i++) {
@@ -318,9 +323,13 @@ BlockForkStrategy.prototype.play = function () {
318323
copyOfBoard.setTileXorO(i, j, this.myLetter);
319324

320325
// Look to see if this creates an opportunity for the opponent to fork.
326+
forkedTile = null;
327+
copyOfBoard.addListener(function (event) {
328+
forkedTile = event;
329+
});
321330
opponentForkStrategy = new ForkStrategy(copyOfBoard, this.opponentLetter);
322-
if (!opponentForkStrategy.play()) {
323-
this.board.setTileXorO(i, j, this.myLetter);
331+
if (opponentForkStrategy.play()) {
332+
this.board.setTileXorO(forkedTile.row, forkedTile.col, this.myLetter);
324333
return true;
325334
}
326335
}
@@ -337,11 +346,10 @@ BlockForkStrategy.prototype.play = function () {
337346
// We add a listener to the copied game board. If the event is fired, it means
338347
// that the ForkStrategy object below was able to make a play. Our listener
339348
// will capture the location where the play was made.
340-
var forkedTile = null;
349+
forkedTile = null;
341350
copyOfBoard.addListener(function (event) {
342351
forkedTile = event;
343352
});
344-
345353
opponentForkStrategy = new ForkStrategy(copyOfBoard, this.opponentLetter);
346354
if (opponentForkStrategy.play()) {
347355
// Instead - we play our piece where the human would've played their piece.
@@ -362,8 +370,11 @@ function CenterStrategy(board, letter) {
362370

363371
CenterStrategy.prototype.play = function () {
364372
console.log("CenterStrategy: Playing center if empty");
365-
if (this.board.getTile(1, 1).unset())
373+
if (this.board.getTile(1, 1).unset()) {
366374
this.board.setTileXorO(1, 1, this.letter);
375+
return true;
376+
}
377+
return false;
367378
};
368379

369380
// Play a corner if the opponent is in the corner and the opposite one is empty.
@@ -385,7 +396,7 @@ OppositeCornerStrategy.prototype.play = function () {
385396
];
386397
for (i = 0; i < corners.length; i++) {
387398
if (this.checkIfUnsetAndSet(corners[i]))
388-
break;
399+
return true;
389400
}
390401
}
391402
else if (this.board.getTile(0, 2).player == this.opponentLetter) {
@@ -396,7 +407,7 @@ OppositeCornerStrategy.prototype.play = function () {
396407
];
397408
for (i = 0; i < corners.length; i++) {
398409
if (this.checkIfUnsetAndSet(corners[i]))
399-
break;
410+
return true;
400411
}
401412
}
402413
else if (this.board.getTile(2, 0).player == this.opponentLetter) {
@@ -407,7 +418,7 @@ OppositeCornerStrategy.prototype.play = function () {
407418
];
408419
for (i = 0; i < corners.length; i++) {
409420
if (this.checkIfUnsetAndSet(corners[i]))
410-
break;
421+
return true;
411422
}
412423
}
413424
else if (this.board.getTile(2, 2).player == this.opponentLetter) {
@@ -418,7 +429,7 @@ OppositeCornerStrategy.prototype.play = function () {
418429
];
419430
for (i = 0; i < corners.length; i++) {
420431
if (this.checkIfUnsetAndSet(corners[i]))
421-
break;
432+
return true;
422433
}
423434
}
424435
return false;
@@ -448,7 +459,7 @@ CornerStrategy.prototype.play = function () {
448459
{row: 2, col: 2}
449460
];
450461
for (var i = 0; i < corners.length; i++) {
451-
if (!this.board.getTile(corners[i].row, corners[i].col).unset()) {
462+
if (this.board.getTile(corners[i].row, corners[i].col).unset()) {
452463
this.board.setTileXorO(corners[i].row, corners[i].col, this.letter);
453464
return true;
454465
}
@@ -472,7 +483,7 @@ EmptySideStrategy.prototype.play = function () {
472483
{row: 2, col: 1}
473484
];
474485
for (var i = 0; i < sides.length; i++) {
475-
if (!this.board.getTile(sides[i].row, sides[i].col).unset()) {
486+
if (this.board.getTile(sides[i].row, sides[i].col).unset()) {
476487
this.board.setTileXorO(sides[i].row, sides[i].col, this.letter);
477488
return true;
478489
}

test/unit/computer.js

+106-54
Original file line numberDiff line numberDiff line change
@@ -42,76 +42,79 @@ describe('ComputerStrategy COMPUTER IS X', function () {
4242

4343
it('should be that X wins (1)', function () {
4444
computer.start();
45-
expect(computerWon).toEqual(false);
46-
expect(playerWon).toEqual(false);
47-
48-
board.setTileXorO(0, 2, "O");
49-
expect(computerWon).toEqual(false);
50-
expect(playerWon).toEqual(false);
45+
expect(computerWon).toBeFalsy();
46+
expect(playerWon).toBeFalsy();
5147

5248
board.setTileXorO(1, 2, "O");
53-
expect(computerWon).toEqual(true);
54-
expect(playerWon).toEqual(false);
55-
expect(wasDraw).toEqual(false);
49+
expect(computerWon).toBeFalsy();
50+
expect(playerWon).toBeFalsy();
51+
52+
board.setTileXorO(2, 2, "O");
53+
expect(computerWon).toBeFalsy();
54+
expect(playerWon).toBeFalsy();
55+
56+
board.setTileXorO(2, 0, "O");
57+
expect(computerWon).toBeTruthy();
58+
expect(playerWon).toBeFalsy();
5659
});
5760

5861
it('should be that X wins (2)', function () {
5962
computer.start();
60-
expect(computerWon).toEqual(false);
61-
expect(playerWon).toEqual(false);
63+
expect(computerWon).toBeFalsy();
64+
expect(playerWon).toBeFalsy();
6265

6366
board.setTileXorO(0, 2, "O");
64-
expect(computerWon).toEqual(false);
65-
expect(playerWon).toEqual(false);
66-
67-
board.setTileXorO(2, 2, "O");
68-
expect(computerWon).toEqual(false);
69-
expect(playerWon).toEqual(false);
67+
expect(computerWon).toBeFalsy();
68+
expect(playerWon).toBeFalsy();
7069

71-
board.setTileXorO(2, 1, "O");
72-
expect(computerWon).toEqual(true);
73-
expect(playerWon).toEqual(false);
70+
board.setTileXorO(1, 2, "O");
71+
expect(computerWon).toBeTruthy();
72+
expect(playerWon).toBeFalsy();
7473
});
7574

7675
it('should be that we play to a draw (1)', function () {
7776
computer.start();
78-
expect(computerWon).toEqual(false);
79-
expect(playerWon).toEqual(false);
77+
expect(computerWon).toBeFalsy();
78+
expect(playerWon).toBeFalsy();
8079

8180
board.setTileXorO(1, 1, "O");
82-
expect(computerWon).toEqual(false);
83-
expect(playerWon).toEqual(false);
81+
expect(computerWon).toBeFalsy();
82+
expect(playerWon).toBeFalsy();
8483

8584
board.setTileXorO(0, 1, "O");
86-
expect(computerWon).toEqual(false);
87-
expect(playerWon).toEqual(false);
85+
expect(computerWon).toBeFalsy();
86+
expect(playerWon).toBeFalsy();
8887

89-
board.setTileXorO(1, 2, "O");
90-
expect(computerWon).toEqual(false);
91-
expect(playerWon).toEqual(false);
88+
board.setTileXorO(1, 0, "O");
89+
expect(computerWon).toBeFalsy();
90+
expect(playerWon).toBeFalsy();
9291

93-
board.setTileXorO(2, 0, "O");
94-
expect(computerWon).toEqual(false);
95-
expect(playerWon).toEqual(false);
96-
expect(wasDraw).toEqual(true);
92+
board.setTileXorO(2, 2, "O");
93+
expect(computerWon).toBeFalsy();
94+
expect(playerWon).toBeFalsy();
95+
expect(wasDraw).toBeTruthy();
9796
});
9897

9998
it('should be that we play to a draw (2)', function () {
10099
computer.start();
101-
expect(computerWon).toEqual(false);
102-
expect(playerWon).toEqual(false);
103-
104-
board.setTileXorO(1, 2, "O");
105-
expect(computerWon).toEqual(false);
106-
expect(playerWon).toEqual(false);
100+
expect(computerWon).toBeFalsy();
101+
expect(playerWon).toBeFalsy();
107102

108103
board.setTileXorO(0, 2, "O");
109-
expect(computerWon).toEqual(false);
110-
expect(playerWon).toEqual(false);
104+
expect(computerWon).toBeFalsy();
105+
expect(playerWon).toBeFalsy();
111106

112-
board.setTileXorO(2, 0, "O");
113-
expect(computerWon).toEqual(true);
114-
expect(playerWon).toEqual(false);
107+
board.setTileXorO(2, 2, "O");
108+
expect(computerWon).toBeFalsy();
109+
expect(playerWon).toBeFalsy();
110+
111+
board.setTileXorO(1, 0, "O");
112+
expect(computerWon).toBeFalsy();
113+
expect(playerWon).toBeFalsy();
114+
115+
board.setTileXorO(2, 1, "O");
116+
expect(computerWon).toBeFalsy();
117+
expect(playerWon).toBeFalsy();
115118
});
116119
});
117120

@@ -156,19 +159,68 @@ describe('ComputerStrategy COMPUTER IS O', function () {
156159
expect(playerWon).toBeUndefined();
157160

158161
board.setTileXorO(0, 0, "X");
159-
expect(computerWon).toEqual(false);
160-
expect(playerWon).toEqual(false);
162+
expect(computerWon).toBeFalsy();
163+
expect(playerWon).toBeFalsy();
161164

162165
board.setTileXorO(0, 2, "X");
163-
expect(computerWon).toEqual(false);
164-
expect(playerWon).toEqual(false);
165-
166-
board.setTileXorO(2, 1, "X");
167-
expect(computerWon).toEqual(false);
168-
expect(playerWon).toEqual(false);
166+
expect(computerWon).toBeFalsy();
167+
expect(playerWon).toBeFalsy();
169168

170169
board.setTileXorO(2, 0, "X");
171-
expect(computerWon).toEqual(true);
172-
expect(playerWon).toEqual(false);
170+
expect(computerWon).toBeTruthy();
171+
expect(playerWon).toBeFalsy();
172+
});
173+
});
174+
175+
describe('ComputerStrategy COMPUTER IS X - Tests for tic-tac-toe-0001', function () {
176+
var board = new TicTacToeBoard();
177+
var game = new TicTacToeGame(board);
178+
var computerPlayer = new Player("COMPUTER", "X");
179+
var computer = new ComputerPlayer(board, game, computerPlayer);
180+
var computerWon, playerWon, wasDraw;
181+
182+
beforeEach(function () {
183+
game.addListener(function (event) {
184+
computerWon = false;
185+
playerWon = false;
186+
wasDraw = false;
187+
188+
if (event.gamestate == TicTacToeGame.WE_HAVE_A_WINNER) {
189+
computerWon = event.winner == "X";
190+
playerWon = event.winner == "O";
191+
}
192+
else if (event.gamestate == TicTacToeGame.PLAYED_TO_A_DRAW) {
193+
wasDraw = true;
194+
}
195+
});
196+
197+
game.addListener(function (event) {
198+
console.log(event.toString());
199+
console.log(event.board.toStringBoard());
200+
})
201+
});
202+
203+
afterEach(function () {
204+
board.reset();
205+
game.reset();
206+
computer.reset(computerPlayer);
207+
});
208+
209+
it('should be that we play to a draw', function() {
210+
computer.start();
211+
212+
board.setTileXorO(1, 2, "O");
213+
expect(board.getTile(1, 1).player).toEqual("X");
214+
expect(computerWon).toBeFalsy();
215+
expect(playerWon).toBeFalsy();
216+
217+
board.setTileXorO(2, 2, "O");
218+
expect(computerWon).toBeFalsy();
219+
expect(playerWon).toBeFalsy();
220+
221+
board.setTileXorO(2, 0, "O");
222+
expect(computerWon).toBeTruthy();
223+
expect(playerWon).toBeFalsy();
224+
173225
});
174226
});

0 commit comments

Comments
 (0)