xxxxxxxxxx
199
// Referencing The Coding Train/ Daniel Shiffman's Product on Tic Tac Toe
// Removing the Minimax Algorithm
// https://thecodingtrain.com/CodingChallenges/154-tic-tac-toe-minimax.html
// https://youtu.be/I64-UTORVfU
// https://editor.p5js.org/codingtrain/sketches/0zyUhZdJD
let board = [
['', '', ''],
['', '', ''],
['', '', '']
];
let ai = 'AI';
let human = 'Justin';
let w, h;
let currentPlayer = human;
let sound1;
let sound2;
let players = ['0', '1'];
let p;
function preload() {
soundFormats('mp3', 'ogg');
sound1 = loadSound('chess.mp3');
sound2 = loadSound('winning.mp3');
}
function setup() {
createCanvas(800, 600);
firstTurn();
w = 250;
h = 150;
p = random(players);
}
function equals3(a, b, c) {
return (a == b && b == c && a != '');
}
function checkWinner() {
let winner = null
for (let i = 0; i < 3; i++) {
if (equals3(board[i][0], board[i][1], board[i][2])) {
winner = board[i][0];
}
}
for (let i = 0; i < 3; i++) {
if (equals3(board[0][i], board[1][i], board[2][i])) {
winner = board[0][i];
}
}
if (equals3(board[0][0], board[1][1], board[2][2])) {
winner = board[0][0];
}
if (equals3(board[2][0], board[1][1], board[0][2])) {
winner = board[2][0];
}
let openSpots = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] == '') {
openSpots++;
}
}
}
if (winner == null && openSpots == 0) {
return 'tie';
} else {
return winner;
}
}
function firstTurn() {
let available = [];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] == '') {
available.push({
i,
j
});
}
}
}
let move = random(available);
board[move.i][move.j] = ai;
currentPlayer = human;
}
function mousePressed() {
if (currentPlayer == human) {
let i;
let j;
if (mouseX < 350 && mouseX > 250) {
i = 0;
} else if (mouseX < 450 && mouseX > 350) {
i = 1;
} else if (mouseX < 550 && mouseX > 450) {
i = 2;
}
if (mouseY > 250 && mouseY < 350) {
j = 0;
} else if (mouseY > 350 && mouseY < 450) {
j = 1;
} else if (mouseY > 450 && mouseY < 550) {
j = 2;
}
// If valid turn
if (board[i][j] == '') {
board[i][j] = human;
sound1.play();
firstTurn();
}
}
}
function draw() {
background(255);
strokeWeight(4);
textSize(50);
fill(0);
if (p == '0') {
text('Player', 60, 80);
text('CPU', 600, 80);
push();
noFill();
ellipse(651,150,100);
pop();
line(75,100,175,200);
line(175,100,75,200);
} else if (p == '1') {
text('Player', 60, 80);
text('CPU', 600, 80);
push();
noFill();
ellipse(125,150,100);
pop();
line(605,100,705,200);
line(705,100,606,200);
}
translate(0,100);
line(w, w, w + 300, w);
line(w, w + 100, w + 300, w + 100);
line(w + 100, h, w + 100, h + 300);
line(h + 300, h, h + 300, h + 300);
for (let j = 0; j < 3; j++) {
for (let i = 0; i < 3; i++) {
let x = 100 * i + 300;
let y = 100 * j + 200;
let spot = board[i][j];
textSize(32);
let r = 30;
if (p == '0') {
if (spot == ai) {
noFill();
ellipse(x, y, r * 2);
} else if (spot == human) {
line(x - r, y - r, x + r, y + r);
line(x + r, y - r, x - r, y + r);
}
} else if (p == '1') {
if (spot == ai) {
line(x - r, y - r, x + r, y + r);
line(x + r, y - r, x - r, y + r);
} else if (spot == human) {
noFill();
ellipse(x, y, r * 2);
}
}
}
}
let result = checkWinner();
if (result != null) {
noLoop();
let resultP = createP('');
resultP.style('font-size', '32pt');
if (result == 'tie') {
resultP.position(365, 140);
resultP.html('Tie!');
} else {
resultP.position(320, 140);
resultP.html(`${result} wins!`);
sound2.play();
}
}
}