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 } }
// Runs a test of the display. Does the following. // Draws the board. Each time you touch one of the screen areas, the screen will paint // an X or an O, depending on whether switch 0 (SW0) is slid up (O) or down (X). // When BTN0 is pushed, the screen is cleared. The test terminates when BTN1 is pushed. void ticTacToeDisplay_runTest() { uint8_t* row = 0; uint8_t* column = 0; ticTacToeDisplay_init(); while(1){ ticTacToeDisplay_touchScreenComputeBoardRowColumn(row , column ); if ((buttons_read() & BUTTON_ISOLATE_BITS) & BUTTONS_ISOLATE_BIT_B0) { ticTacToeDisplay_clear(); } if ((buttons_read() & BUTTON_ISOLATE_BITS) & BUTTONS_ISOLATE_BIT_B1) { ticTacToeDisplay_gameOver(); break; } } }
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; } }