void ticTacToeDisplay_runTest() { while(1) //until button 1 is pressed { while(buttons_read() != (TICTACTOEDISPLAY_BTN0_ON))//until button 0 is pressed { if(buttons_read() == (TICTACTOEDISPLAY_BTN1_ON))//button has been pressed { display_setTextSize(TICTACTOEDISPLAY_TEXT_SIZE); // this sets the text size display_setCursor(TICTACTOEDISPLAY_HALF_WIDTH-TICTACTOEDISPLAY_ONE_THIRD_WIDTH, //position x coordinate of cursor TICTACTOEDISPLAY_HALF_HEIGHT - TICTACTOEDISPLAY_ONE_SIXTH_HEIGHT);//position y coordinate of cursor display_println("Game Over!");//print to the lcd return;//return out of both loops immediately } if(display_isTouched()) { display_clearOldTouchData(); // Throws away all previous touch data. uint8_t row, column; ticTacToeDisplay_touchScreenComputeBoardRowColumn(&row, &column);//compute location of square if(switches_read() == TICTACTOEDISPLAY_SWITCH0_ON)//SW0 is up { ticTacToeDisplay_drawO(row, column);//draw O character } if(switches_read() == TICTACTOEDISPLAY_SWITCH0_OFF)//SW0 is down { ticTacToeDisplay_drawX(row, column);//draw X character } } } ticTacToeDisplay_init();//clear the screen } }
void ticTacToeControl_tick() //actions and transitions of state machine { debugStatePrint(); //print state for debugging switch (TTT_State) //state machine actions { case TTT_init_st: //inital entry state readTimer = 0; //initialize read timer firstTouchTimer = 0; //set to zero adTimer = 0; //set to zerio break; case TTT_draw_instructions_st: //draw print the instructions display_setTextSize(TEXT_SIZE); //how big display_setTextColor(DISPLAY_WHITE);//set the color display_setCursor(INSTRUCTIONS_LOCATION_X, INSTRUCTIONS_LOCATION_Y); display_println(INSTRUCTIONS);//print instructions break; case TTT_let_user_read_st: readTimer++; //increase read timer break; case TTT_wait_first_touch_st: firstTouchTimer++; //increment first touch wait timer break; case TTT_wait_for_settle_st: adTimer++; //increment timer for settling break; case TTT_computer_play_st: if (playerX) //computer is O { minimax_computeNextMove(¤tBoard, false, &controlTouchRow, &controlTouchColumn); //its always computing computers move, so just set the bool to false ticTacToeDisplay_drawO(controlTouchRow, controlTouchColumn); //mark that board space currentBoard.squares[controlTouchRow][controlTouchColumn] = MINIMAX_OPPONENT_SQUARE; } else //computer went first, is X { minimax_computeNextMove(¤tBoard, false, &controlTouchRow, &controlTouchColumn); //its always computing computers move, so just set the bool to false ticTacToeDisplay_drawX(controlTouchRow, controlTouchColumn); //mark that board space currentBoard.squares[controlTouchRow][controlTouchColumn] = MINIMAX_OPPONENT_SQUARE; } break; case TTT_user_play_st: //read button value here, and check in transitions newBTNValue = buttons_read(); invalidMoveFlag = false; // reset the flag //reset wait for settle timer adTimer = 0; if (playerX) //player went first, is X { ticTacToeDisplay_touchScreenComputeBoardRowColumn(&controlTouchRow, &controlTouchColumn); if(currentBoard.squares[controlTouchRow][controlTouchColumn] == MINIMAX_EMPTY_SQUARE) //check square is empty { ticTacToeDisplay_drawX(controlTouchRow, controlTouchColumn); //draw the x //mark that board space currentBoard.squares[controlTouchRow][controlTouchColumn] = MINIMAX_PLAYER_SQUARE; //mark move in board } else invalidMoveFlag = true; //player tried to go in occupied square } else //player is O { ticTacToeDisplay_touchScreenComputeBoardRowColumn(&controlTouchRow, &controlTouchColumn); if(currentBoard.squares[controlTouchRow][controlTouchColumn] == MINIMAX_EMPTY_SQUARE) //check square is empty { ticTacToeDisplay_drawO(controlTouchRow, controlTouchColumn); //draw the O //mark that board space currentBoard.squares[controlTouchRow][controlTouchColumn] = MINIMAX_PLAYER_SQUARE; //mark move in board } else invalidMoveFlag = true; //player tried to go in occupied square } break; case TTT_wait_touch_st: //just wait case TTT_end_st: //read button value here, and check in transitions //only reset at end game newBTNValue = buttons_read(); default: break; } ////////////////////////////////////////////////////////////////////////////////////////////////// switch (TTT_State) //state machine transitions { case TTT_init_st: //inital entry state TTT_State = TTT_draw_instructions_st; //move to instructions state break; case TTT_draw_instructions_st: TTT_State = TTT_let_user_read_st; //go straight to reading after instructions are drawn break; case TTT_let_user_read_st: if (readTimer == TTT_READ_EXPIRED) { TTT_State = TTT_wait_first_touch_st; //wait for touching ticTacToeDisplay_reset();//draw empty board. mealey action } break; case TTT_wait_first_touch_st: { if (firstTouchTimer == TTT_FIRST_TOUCH_EXPIRED) //if the wait is expired { //computer goes first. computer is X TTT_State = TTT_computer_play_st; playerX = false; } else if (display_isTouched()) //human touched a spot { //human goes first. is X TTT_State = TTT_wait_for_settle_st; playerX = true; } } break; case TTT_wait_for_settle_st: if (adTimer == ADTIMER_EXPIRE) //gave touch screen time to settle { TTT_State = TTT_user_play_st; //go to user play } break; case TTT_computer_play_st: // if board full, go to end game if (ticTacToeControl_checkEndGame(¤tBoard, false)) //pass in the board, does matter what the bool is, just need an endgame score TTT_State = TTT_end_st; else TTT_State = TTT_wait_touch_st; //go to place to wait for player to touch break; case TTT_user_play_st: if (ticTacToeControl_checkEndGame(¤tBoard, true)) //pass in the board, does matter what the bool is, just need an endgame score TTT_State = TTT_end_st; else if(invalidMoveFlag) //player tried to go in occupied space { TTT_State = TTT_wait_touch_st; //wait for another touch } else TTT_State = TTT_computer_play_st; //go to computer move break; case TTT_wait_touch_st: if (ticTacToeControl_checkEndGame(¤tBoard, playerX)) //pass in the board, does matter what the bool is, just need an endgame score TTT_State = TTT_end_st; else if (display_isTouched()) //done waiting { TTT_State = TTT_wait_for_settle_st; //go settle state } break; case TTT_end_st: ticTacToeControl_checkResetBTN(); //check if button has been pressed, break; default: printf("ERROR: missed transition\n\r"); break; } }
// After a touch has been detected and after the proper delay, this sets the row and column arguments // according to where the user touched the board. void ticTacToeDisplay_touchScreenComputeBoardRowColumn(uint8_t* row, uint8_t* column) { int16_t x = 0; int16_t y = 0; uint8_t z = 0; uint8_t rowval; uint8_t columnval; if(display_isTouched()) { display_clearOldTouchData(); utils_msDelay(50); display_getTouchedPoint(&x, &y, &z); *row = 0; *column = 0; // column 0 if (x <= display_width()/3) { //row 0 if (y <= display_height()/3) { rowval = 0; columnval = 0; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 0 column 0 \n\r" ); } //row 1 else if (y <= 2* display_height()/3) { rowval = 1; columnval = 0; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 1 column 0 \n\r" ); } //row 2 else { rowval = 2; columnval = 0; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 2 column 0 \n\r" ); } } // column 1 else if (x <= 2*(display_width()/3) ) { //row 0 if (y <= display_height()/3) { rowval = 0; columnval = 1; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 0 column 0 \n\r" ); } //row 1 else if (y <= 2* display_height()/3) { rowval = 1; columnval = 1; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 1 column 0 \n\r" ); } //row 2 else { rowval = 2; columnval = 1; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 2 column 0 \n\r" ); } } // column 2 else { //row 0 if (y <= display_height()/3) { rowval = 0; columnval = 2; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 0 column 0 \n\r" ); } //row 1 else if (y <= 2* display_height()/3) { rowval = 1; columnval = 2; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 1 column 0 \n\r" ); } //row 2 else { rowval = 2; columnval = 2; if (ticTacToeDisplay_SW()) { ticTacToeDisplay_drawO(rowval, columnval); } else { ticTacToeDisplay_drawX(rowval, columnval); } printf("row 2 column 0 \n\r" ); } } } }