James Cameron
Published © GPL3+

Google Sheets Noughts And Crosses

I show you how I made Noughts and Crosses within Google Sheets.

BeginnerFull instructions provided1 hour843
Google Sheets Noughts And Crosses

Things used in this project

Software apps and online services

Google Sheets
Google Sheets

Story

Read more

Code

Apps Script Code

JavaScript
This is the full code for Google Apps Script
function drawBoard() {
 mv = ["","X","O"] //Different possible pieces
 letters = SpreadsheetApp.getActiveSheet().getRange("E2:M2").getValues()[0]; //Get a list of cells E2 to M2 
 a = letters[0]; //Define each letter
 b = letters[1];
 c = letters[2];
 d = letters[3];
 e = letters[4];
 f = letters[5];
 g = letters[6];
 h = letters[7];
 i = letters[8];
 values = [[[mv[a]],[mv[b]],[mv[c]]],[[mv[d]],[mv[e]],[mv[f]]],[[mv[g]],[mv[h]],[mv[i]]]] //Make a 3x3 array of the grid.
 SpreadsheetApp.getActiveSheet().getRange("A1:C3").setValues(values); //Enter it into the board
}

function onEdit(e) {
  drawBoard();
  checkWin();
}

function checkWin() {
 b = SpreadsheetApp.getActiveSheet().getRange("E2:M2").getValues()[0]; //Gets the saved board
 for (var i = 1; i < b.length-1; i=i+3) { //Goes through board in 3s (rows)
   if (b[i]==b[i-1] && b[i]==b[i+1] && b[i] > 0) { //Checks if current row is all equal and greater than 0;
       SpreadsheetApp.getActiveSheet().getRange("O2").setValue(b[i]); //Outputs the winner     
       return b[i]; //Outputs the winner to the sheet and ends the function
   }
 }
 for (var i = 0; i < 3; i++) { //Goes through the first 3 values (columns)
   if (b[i]==b[i+3] && b[i]==b[i+6] && b[i] > 0) { //Checks if a column are all equal and greater than 0
     SpreadsheetApp.getActiveSheet().getRange("O2").setValue(b[i]); //Outputs the winner
     return b[i]; //Outputs the winner to the sheet and ends the function
   }
 }
 if (b[0]==b[4] && b[0] == b[8] && b[0]>0) { //Checks for diagonal win from top left to bottom right
   SpreadsheetApp.getActiveSheet().getRange("O2").setValue(b[0]);
   return b[0]; //Outputs the winner to the sheet and ends the function
 }
 if (b[2]==b[4] && b[2] == b[6] && b[2]>0) {  //Checks for diagonal win from bottom left to top right
   SpreadsheetApp.getActiveSheet().getRange("O2").setValue(b[2]);
   return b[2]; //Outputs the winner to the sheet and ends the function
 }
 SpreadsheetApp.getActiveSheet().getRange("O2").setValue(0);
 return 0; //Outputs 0 if no winner can be found
}

function turn() { //This is a function to check whos turn it is and change it
 turn = SpreadsheetApp.getActiveSheet().getRange("N2:N2").getValues()[0][0];
 if (turn==1) {
   SpreadsheetApp.getActiveSheet().getRange("N2").setValue(2);
 } else if (turn==2) {
   SpreadsheetApp.getActiveSheet().getRange("N2").setValue(1);
 }
 return(turn); //Returns the players turn
}

function write(sq) { //This function takes an for the cell that a player will play in it then checks if something is already in that cell, if its clear then it writes whoevers turn it is.
 col = sq[0]; //Finds the column
 ce = SpreadsheetApp.getActiveSheet().getRange((col+3)).getValue(); //Gets whether something is there
 if (ce == 0) {
   SpreadsheetApp.getActiveSheet().getRange(sq).setValue(turn()); //Writes the players turn from the turn function we created earlier.
   SpreadsheetApp.getActiveSheet().getRange((col+3)).setValue("1");
   onEdit(); //Calls onEdit() so that the board is redrawn
 }
}

function tl() {
  write("E2");
}
function tm() {
  write("F2");
}
function tr() {
  write("G2");
}
function ml() {
  write("H2");
}
function mm() {
  write("I2");
}
function mr() {
  write("J2");
}
function bl() {
  write("K2");
}
function bm() {
  write("L2");
}
function br() {
  write("M2");
}

function reset() {
 SpreadsheetApp.getActiveSheet().getRange("E2:M3").setValue("0"); //Clear the stored version of the board
 SpreadsheetApp.getActiveSheet().getRange("N2").setValue("1"); //Set it to player ones turn
 SpreadsheetApp.getActiveSheet().getRange("O2").setValue("0"); //Clear the win detection
 onEdit(); //Update board
}

function debug() {
  db = SpreadsheetApp.getActiveSheet().getRange("F5").getValue();
  if (db == 0) {
    SpreadsheetApp.getActiveSheet().hideColumns(5, 11);
    SpreadsheetApp.getActiveSheet().getRange("F5").setValue(1);
  } else {
    SpreadsheetApp.getActiveSheet().unhideColumn(SpreadsheetApp.getActiveSheet().getRange("E1:O1"));
    SpreadsheetApp.getActiveSheet().getRange("F5").setValue(0);
  }
}

Credits

James Cameron
3 projects • 7 followers

Comments