Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% win rate #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 155 additions & 3 deletions players/4/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
export default class Player {
static get name () {
return `Andy is Awesome`;
return `Team 4 rules`;
}

/**
Expand All @@ -28,7 +28,159 @@ export default class Player {
* 6 | 7 | 8
*/
static makeMove(token, gameState) {
// Just pick the first open square.
return gameState.indexOf('-');
let move;

let antitoken;
if (token === 'X') {
antitoken = 'O'
} else {
antitoken = 'X'
}

if (gameState[4] === '-') {
move = 4;
}

gameState.forEach((item, index) => {
if (item === '-') {
neighborObject[index].forEach(neighbor => {
if (gameState[neighbor] == token) {
move = Number(index)
}
})
}
})

if (checkForDiagonalVic(antitoken, gameState)) {
move = checkForDiagonalVic(antitoken, gameState)
}
if (checkForHorizVic(antitoken, gameState)) {
move = checkForHorizVic(antitoken, gameState)
}
if (checkForVertVic(antitoken, gameState)) {
move = checkForVertVic(antitoken, gameState)
}

if (checkForDiagonalVic(token, gameState)) {
move = checkForDiagonalVic(token, gameState)
}
if (checkForHorizVic(token, gameState)) {
move = checkForHorizVic(token, gameState)
}
if (checkForVertVic(token, gameState)) {
move = checkForVertVic(token, gameState)
}

return move || gameState.indexOf('-');
}
};

const neighborObject = {
'0': ['1','3','4'],
'1': ['0', '2', '3', '4', '5'],
'2': ['1', '4', '5'],
'3': ['0', '1', '4', '6', '7'],
'4': ['0', '1', '2', '3', '4', '5', '6', '7', '8'],
'5': ['1', '2', '4', '7', '8'],
'6': ['3', '4', '7'],
'7': ['3', '4', '5', '6', '8'],
'8': ['4', '5', '7'],
}

// tokenObject = {
// '0': 0,
// '1': 0,
// '2': 0,
// '3': 0,
// '4': 0,
// '5': 0,
// '6': 0,
// '7': 0,
// '8': 0,
// }

function checkForDiagonalVic(token, gameState) {
if (gameState[0] === token && gameState[4] === token) {
return 8;
}
if (gameState[0] === token && gameState[8] === token) {
return 4;
}
if (gameState[4] === token && gameState[8] === token) {
return 0;
}

if (gameState[2] === token && gameState[4] === token) {
return 6;
}
if (gameState[2] === token && gameState[6] === token) {
return 4;
}
if (gameState[4] === token && gameState[6] === token) {
return 2;
}
}

function checkForHorizVic(token, gameState) {
if (gameState[0] === token && gameState[1] === token) {
return 2;
}
if (gameState[0] === token && gameState[2] === token) {
return 1;
}
if (gameState[1] === token && gameState[2] === token) {
return 0;
}

if (gameState[3] === token && gameState[4] === token) {
return 5;
}
if (gameState[3] === token && gameState[5] === token) {
return 4;
}
if (gameState[4] === token && gameState[5] === token) {
return 3;
}

if (gameState[6] === token && gameState[7] === token) {
return 8;
}
if (gameState[6] === token && gameState[8] === token) {
return 7;
}
if (gameState[7] === token && gameState[8] === token) {
return 6;
}
}

function checkForVertVic(token, gameState) {
if (gameState[0] === token && gameState[3] === token) {
return 6;
}
if (gameState[0] === token && gameState[6] === token) {
return 3;
}
if (gameState[3] === token && gameState[6] === token) {
return 0;
}

if (gameState[1] === token && gameState[4] === token) {
return 7;
}
if (gameState[1] === token && gameState[7] === token) {
return 4;
}
if (gameState[4] === token && gameState[7] === token) {
return 1;
}

if (gameState[2] === token && gameState[5] === token) {
return 8;
}
if (gameState[5] === token && gameState[8] === token) {
return 2;
}
if (gameState[2] === token && gameState[8] === token) {
return 5;
}
}
3 changes: 3 additions & 0 deletions players/4/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Player from './index';

Player.makeMove('X', ['X', '-', '-', '-', '-', '-', '-', '-', '-'])