Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Created November 20, 2014 12:50
Show Gist options
  • Save Shiggiddie/6c0a70ec1eba06bde255 to your computer and use it in GitHub Desktop.
Save Shiggiddie/6c0a70ec1eba06bde255 to your computer and use it in GitHub Desktop.
After writing tic-tac-toe, there were some subtle improvements possible, HW was assigned and these are the answers
//1) Fix computer algorithm (assigning to Anth)
// In Player's .makeMove method:
if (index == null) {
// No block? Try for center...
//console.log('computer is attempting to center');
if (grid.vectors[4].value == null) {
index = 4;
}
}
//2) Add start-of-game ability to set up human v human, human v computer, or computer v computer (assigning to Dan)
Game.prototype.setUpPlayers = function() {
console.log('Choose your player option: 1 for human v human, 2 for human v computer, 3 for computer v computer');
var invalidMove = true;
while (invalidMove) {
var choice = parseInt(prompt('Enter a number: '));
if ([1,2,3].indexOf(choice) > -1) {
if (choice == 1) {
this.players = [new Player('human', 'X'), new Player('human', 'O')]
}
else if (choice == 2) {
this.players = [new Player('human', 'X'), new Player('computer', 'O')]
}
else {
this.players = [new Player('computer', 'X'), new Player('computer', 'O')]
}
invalidMove = false;
}
else {
console.log("Please enter a valid number");
}
}
}
Game.prototype.play = function() {
console.log('Welcome to Tic-Tac-Toe!\n' +
'To move, Enter a number from 1-9 when prompted.');
this.setUpPlayers();
//...
//3) Add a console log that informs the player of all of their possible remaining moves at the start of their turn (assigning to Rod)
// In Player's .makeMove method, under if human:
if (this.type == 'human') {
open_indexes = grid.vectors.filter(function(vector) {
return vector.value == null;
}).map(function(vector, i) {
return grid.vectors.indexOf(vector)+1;
});
while (invalidMove) {
var index = prompt('Enter a number from this list of available spaces ' + open_indexes + ': ') - 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment