cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              /*
Human = 1
AI = 0
ABC


*/
var deleteMe;
var board = [];
var player = 0;

function setup() {
  createCanvas(400, 400);
  background(200);
  fill(100);
  //rect(50,50,50,50);
  resetBoard(4);
  drawBoard2(4);
  //gamePlay();
  //getInputFromAI();
  //console.log("player= " +player); 
  //resetInputFromHuman(4);
  //getInputFromAI(); 
  
}

function draw() {

  //console.log("player= " +player);
  if (player===0) {
    getInputFromAI();
  }
  
  if (player==1) {
    readInputFromHuman();
  }
  colorBoard(4);
} 

/*
function gamePlay () {
  var foundWinner = false;
  do {
 
    if (player===0) {
    getInputFromAI();
  } 
  
  if (player==1) {
    readInputFromHuman();
  } 
  colorBoard(4);
 
  foundWinner = checkWinner(4);
  foundWinner = true;
  }
  while (!foundWinner); //loop while no winner
  textSize(30);
  text("STOP",100,100);
}
*/
 
function getInputFromAI() {
  var validValue = false;
  do {
  var inputFromAI = Math.random()*17+0.9;
  inputFromAI=parseInt(inputFromAI);
  //console.log("AI  choose: " +inputFromAI);
  validValue = checkInput(inputFromAI);
     }
  while (!validValue); // loop when validValue = false
  resetInputFromHuman(4);
  player = 1; //change player to human
}


function resetBoard(n) { //nxn matrix of 0.5-values
  for (var i=0;i<n;i++) {
    board.push(new Array(n).fill(0.5)); 
    }
  //console.log("New board =" +board); 
}

 
function resetInputFromHuman(n) { //n= number of rows
  input = createInput();
  input.position(50, 100+n*50);
  button = createButton('send');
  button.position(input.x + input.width, 100+n*50);
  button.mousePressed(getInputFromHuman);
  input.value('');
  greeting = createElement('h3', 'Your move');
  greeting.position(50, 50+n*50);
}


function readInputFromHuman () {
  button.mousePressed(getInputFromHuman);
}

function getInputFromHuman() {
  var validValue = false;
  do { //wait for valid value
    var inputValue = input.value();
    inputValue = parseInt(inputValue); //forces input from string to integer
    validValue = checkInput(inputValue); //turns TRUE if value is ok.
    resetInputFromHuman(4); //resets input, ready for new input
  } 
  while (!validValue); // loop when validValue = false
  player = 0; //change player to AI
}


function checkInput(inputValue) {
  var i;
  if (inputValue/4<=1) { //input between 1-4
    i=0; //row number 0
  }
   if (inputValue/4>1) { //input between 5-8
    i=1; //row number 1
  }
   if (inputValue/4>2) { //input between 9-12
    i=2; //row number 2
  }
  if (inputValue/4>3) { //input between 13-16
    i=3; //row number 3
  }
  //console.log(i);
 var j=inputValue-i*4-1;
 //console.log(j);
  //console.log("Value af choosen position:" +board[i][j]);
  if (board[i][j]==0.5 && inputValue<17 && inputValue>0 && Number.isInteger(inputValue)) {
    board[i][j]=player;
   // console.log("Current board: "+board);
    return('true'); 
  }
  else {
    fill(0);
    text("try again",50,40);
    fill(150);
  }
}

 

function checkWinner(n) { // nxn matrix
 for (var x=0;x<2;x++) { //check for each player, AI=0, human=1
  for (var i=0;i<n;i++) { //check for all columns
    for(var j=0;j<n;j++) { //check for all rows
      console.log(x);
      if (board[j][i]===x && board[j][i+1]===x && board[j][i-1]===x) { //horizontal
        console.log("horizontal"); 
        return('true');
      }  
      if (board[j][i]===x && board[j+1][i+1]===x && board[j-1][i-1]===x) { //diagonal up down
        console.log("d1");
        return('true');
      } 
      if (board[j][i]===x && board[j-1][i+1]===x && board[j+1][i-1]===x) { //diagonal down up
        console.log("d2");
        return('true');
      }         
      if (board[j][i]===x && board[j-1][i]===x && board[j+1][i]===x) { //vertical
        console.log("vertical");
        return('true');
      }   
    }    
  }
 }
}


function nextAction() {}

/*function drawBoard(n) {
  fill(0);
  for (var i=0;i<n+1;i++) { //vandrette linjer
    line(100,100+i*100,100+n*100,100+i*100); //max = 500
  }
  for (var j=0;j<n+1;j++) { //lodrette linjer
    line(100+j*100,100,100+j*100,100+n*100);
  }
}*/

function drawBoard2(n) {
  fill(150);
  stroke(0);
  for (var i=0;i<n;i++) { 
    for (var j=0;j<n;j++) { 
      var x= i *50+50;
      var y= j*50+50;
      rect(x,y,45,45);
      var field = 4*j+i+1; //find number for each field
      fill(0); //textcolor black
      text(field,x+5,y+15); //write number in field
      fill(150); //color reset to grey
    }
  }
} 

function colorBoard(n) {
  fill(200); //erase "try again" message
  noStroke();
  rect(50,10,100,35);
  stroke(0); //black outline
  for (var i=0;i<n;i++) { 
    for (var j=0;j<n;j++) { 
      if (board[j][i]===0) { //if AI has field
      fill(255,0,0); //red
      var x= i *50+50;
      var y= j*50+50;
      rect(x,y,45,45);  
      }
      if (board[j][i]==1) { //if player has field
      fill(0,255,0); //green
      var x2 = i *50+50;
      var y2= j*50+50;
      rect(x2,y2,45,45);  
      }
    }
  }
} 
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console