Announcement

Collapse
No announcement yet.

Arduino TicTacToe Picaso Serial

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arduino TicTacToe Picaso Serial

    I adapted a Tic-Tac-Toe sketch written by Elizabeth Greene to make it run on a 4d screen. I figured I would share it here, since it's a fun little time killer.




    //---------------- Picaso librarys ---------------#include "Picaso_Serial_4DLib.h" //#include "Picaso_const4D.h" ////------------------------------------------------ //-------Various Arduino setups-------------------//#define DisplaySerial Serial // Uncomment if using AR shield or Pins 0, 1 and comment out Serial2 line#define DisplaySerial Serial2 // Mega 2560 digital pins 16 & 17// Uncomment the following 2 lines and comment out Serial2 line if using sofware serial and set pins accordingly//#include // //SoftwareSerial DisplaySerial(2,3) ; // pin 2 = TX of display, pin3 = RXPicaso_Serial_4DLib Display(&DisplaySerial); ////------------------------------------------------#define XMARK 1#define OMARK 0#define NOMARK -1#define DRAW -2char mdl[20]; // handle for display modelint mnt; // handle for sd mountint Board[10];int turndicator = OMARK;void setup(){ // ------------ Initialize screen -------------- // Call these functions in this order.. // // Do not address the screen before or // // within this group. // delay(2000); // wait for screen to power up DisplaySerial.begin(255000); // set baud to whatever screen is running Display.TimeLimit4D = 5000; // Display.sys_GetModel(mdl); // //----------^--- End initialize ---^------------ //------- clear screen and display model ------- Display.gfx_ScreenMode(PORTRAIT); // set orientation Display.gfx_Cls(); // Display.gfx_MoveTo(0,0); // Display.putstr("Model="); // Display.putstr(mdl); // delay(1500); // optional pause to display model //-------------------------------------------- //---Mount uSD-Uncomment if using uSD card------ Display.gfx_Cls(); // Display.putstr("Mounting..."); // mnt = Display.file_Mount(); // Display.txt_MoveCursor(1,0); // if (mnt) Display.putstr("Mounted"); // else Display.putstr("Not Mounted"); // delay(1500); // optional pause to display mount status //---------------------------------------------- //--------- Enable touch and touch region------- Display.touch_Set(TOUCH_ENABLE); // Display.touch_DetectRegion(0,0, 399, 239); // Display.gfx_Cls(); // //---------------------------------------------- Display.txt_Height(2); Display.txt_Width(2); DrawSplash(); delay(3000); ResetGame();} void DrawSplash() { DrawBoard(); drawX(5); delay(500); drawO(1); delay(500); drawX(4); delay(500); drawO(3); delay(500); drawX(6); Display.gfx_MoveTo(0,360); Display.putstr("TIC-TAC-TOE");} void loop(){ int state; state = Display.touch_Get(0); int MapX1 = 239; int MapX2 = 0; int MapY1 = 319; int MapY2 = 0; //holds x y coordinates int p; int x; int y; int q; x = Display.touch_Get(TOUCH_GETX); y = Display.touch_Get(TOUCH_GETY); p = map(x, 239, 0, MapX1, MapX2); q = map(y, 399, -80, MapY1, MapY2); // we have some minimum pressure we consider 'valid' // pressure of 0 means no pressing! if (state == TOUCH_RELEASED){ int pos = GetFingerPos(p,q); if (Board[pos] == -1) { if (turndicator == OMARK ) { drawO(pos); Board[pos] = turndicator; turndicator = XMARK; } else { drawX(pos); Board[pos] = turndicator; turndicator = OMARK; } int winner = CheckWinner(); if (winner != NOMARK) { Display.gfx_MoveTo(0,360); if (winner == XMARK) { Display.putstr("X WINS"); } if (winner == OMARK) { Display.putstr("O WINS"); } if (winner == DRAW) { Display.putstr("DRAW"); } delay(2000); ResetGame(); } } // Tft.fillCircle(p.x,p.y,2,RED); }} int CheckWinner() { int winner = NOMARK; //Surely there is a better way? //Across if (Board[1] == Board[2] && Board[1] == Board[3] && winner == NOMARK) { winner=Board[1]; } if (Board[4] == Board[5] && Board[4] == Board[6] && winner == NOMARK) { winner=Board[4]; } if (Board[7] == Board[8] && Board[7] == Board[9] && winner == NOMARK) { winner=Board[7]; } //down if (Board[1] == Board[4] && Board[1] == Board[7] && winner == NOMARK) { winner=Board[1]; } if (Board[2] == Board[5] && Board[2] == Board[8] && winner == NOMARK) { winner=Board[2]; } if (Board[3] == Board[6] && Board[3] == Board[9] && winner == NOMARK) { winner=Board[3]; } //diagonal if (Board[1] == Board[5] && Board[1] == Board[9] && winner == NOMARK) { winner=Board[1]; } if (Board[3] == Board[5] && Board[3] == Board[7] && winner == NOMARK) { winner=Board[3]; } // finally, check and see if all of the spaces are occupied.. if (winner == NOMARK && Board[1] > -1 && Board[2] > -1 && Board[3] > -1 && Board[4] > -1 && Board[5] > -1 && Board[6] > -1 && Board[7] > -1 && Board[8] > -1 && Board[9] > -1 ) { winner = DRAW; } return winner;} void ResetGame() { DrawBoard(); // Draw the board Display.gfx_RectangleFilled(0,360,239,399,BLACK); for (int i=0;i

  • #2


    Few Screen shots would be nice or perhaps even a short video
    Atilla

    Comment


    • #3


      A short video it is!
      http://[video=youtube_share;yIQrHcuH...ature=youtu.be[/video]

      Comment

      Working...
      X