Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f0xx.h"
- #include "stm32f0_discovery.h"
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include "fifo.h"
- #define UNK -1
- #define NON_INTR 0
- #define INTR 1
- int __io_putchar(int ch);
- static int putchar_nonirq(int ch);
- void step3(void);
- void step4(void);
- void step5(void);
- #define COL1 (1 << 0)
- #define COL2 (1 << 1)
- #define COL3 (1 << 2)
- #define COL4 (1 << 3)
- #define ROW1 (1 << 4)
- #define ROW2 (1 << 5)
- #define ROW3 (1 << 6)
- #define ROW4 (1 << 7)
- #define SAMPLE_TIME_MS 10
- #define SAMPLE_COUNT (SAMPLE_TIME_MS)
- #define THRESHOLD_TIME_MS 2
- #define THRESHOLD (THRESHOLD_TIME_MS)
- #define KEY_PRESS_MASK 0b11000111
- #define KEY_REL_MASK 0b11100011
- static struct fifo input_fifo; // input buffer
- static struct fifo output_fifo; // output buffer
- int interrupt_mode = UNK; // which version of putchar/getchar to use.
- int echo_mode = 0; // should we echo input characters?
- int ships[16][16];
- int player_1 = 1;
- char input[5];
- char X[3];
- char Y[3];
- int counter =0;
- char coord[4] = {'\0', '\0', '\0', '\0',};
- int t_i = 0;
- bool win = false;
- int opp_hit = 0;
- int hit_opp = 0;
- int phase = 1;
- char player = '\0';
- char my_turn = '\0';
- char opponent_ready = 'N';
- char phase2_input[7] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'};
- char phase3_input[5] = {'\0', '\0', '\0', '\0', '\0'};
- bool ship5_set = false;
- bool ship4_set = false;
- bool ship2_set = false;
- bool ship3_1_set = false;
- bool ship3_2_set = false;
- int input_index = 0;
- int grid[16][16];
- //=============================================================================
- //=======================================================================
- // Simply write a string one char at a time.
- //=======================================================================
- static void putstr(const char *s) {
- while(*s)
- __io_putchar(*s++);
- }
- //=======================================================================
- // Insert a character and echo it.
- // (or, if it's a backspace, remove a char and erase it from the line).
- // If echo_mode is turned off, just insert the character and get out.
- //=======================================================================
- static void insert_echo_char(char ch) {
- if (ch == '\r')
- ch = '\n';
- if (!echo_mode) {
- fifo_insert(&input_fifo, ch);
- return;
- }
- if (ch == '\b' || ch == '\177') {
- if (!fifo_empty(&input_fifo)) {
- char tmp = fifo_uninsert(&input_fifo);
- if (tmp == '\n')
- fifo_insert(&input_fifo, '\n');
- else if (tmp < 32)
- putstr("\b\b \b\b");
- else
- putstr("\b \b");
- }
- return; // Don't put a backspace into buffer.
- } else if (ch == '\n') {
- __io_putchar('\n');
- } else if (ch == 0){
- putstr("^0");
- } else if (ch == 28) {
- putstr("^\\");
- } else if (ch < 32) {
- __io_putchar('^');
- __io_putchar('A'-1+ch);
- } else {
- __io_putchar(ch);
- }
- fifo_insert(&input_fifo, ch);
- }
- //-----------------------------------------------------------------------------
- // Section 6.2
- //-----------------------------------------------------------------------------
- // This should should perform the following
- // 1) Enable clock to GPIO port A
- // 2) Configure PA9 and PA10 to alternate function to use a USART
- // Note: Configure both MODER and AFRL registers
- // 3) Enable clock to the USART module, it is up to you to determine
- // which RCC register to use
- // 4) Disable the USART module (hint UE bit in CR1)
- // 5) Configure USART for 8 bits, 1 stop bit and no parity bit
- // 6) Use 16x oversampling
- // 7) Configure for 115200 baud rate
- // 8) Enable the USART for both transmit and receive
- // 9) Enable the USART
- // 10) Wait for TEACK and REACK to be set by hardware in the ISR register
- // 11) Set the 'interrupt_mode' variable to NON_INTR
- void tty_init(void) {
- // Disable buffers for stdio streams. Otherwise, the first use of
- // each stream will result in a *malloc* of 2K. Not good.
- setbuf(stdin,0);
- setbuf(stdout,0);
- setbuf(stderr,0);
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER |= GPIO_MODER_MODER9_1;
- GPIOA->MODER &= ~GPIO_MODER_MODER9_0;
- GPIOA->MODER |= GPIO_MODER_MODER10_1;
- GPIOA->MODER &= ~GPIO_MODER_MODER10_0;
- GPIOA->AFR[1] |= 0x00000110;
- RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
- USART1->CR1 &= ~(USART_CR1_UE);
- USART1->CR1 &= ~(USART_CR1_OVER8);
- USART1->CR2 &= ~(USART_CR2_STOP);
- USART1->CR1 &= ~(USART_CR1_M);
- USART1->CR1 &= ~(USART_CR1_PCE);
- USART1->BRR = 417;
- USART1->CR1 |= USART_CR1_TE;
- USART1->CR1 |= USART_CR1_RE;
- USART1->CR1 |= (USART_CR1_UE);
- while (!(((USART1->ISR & USART_ISR_REACK) == USART_ISR_REACK) && ((USART1->ISR & USART_ISR_TEACK) == USART_ISR_TEACK)));
- interrupt_mode = NON_INTR;
- // Student code goes here...
- }
- //=======================================================================
- // Enable the USART RXNE interrupt.
- // Remember to enable the right bit in the NVIC registers
- //=======================================================================
- void enable_tty_irq(void) {
- // Student code goes here...
- USART1->CR1 |= USART_CR1_RXNEIE;
- NVIC->ISER[0] |= 1 << USART1_IRQn;
- interrupt_mode = INTR;
- }
- //-----------------------------------------------------------------------------
- // Section 6.5
- //-----------------------------------------------------------------------------
- // See lab document for description
- //=======================================================================
- // IRQ invoked for USART1 activity.
- void USART1_IRQHandler(void) {
- // Student code goes here...
- if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) {
- insert_echo_char(USART1->RDR & 0xff);
- }
- if((USART1->ISR & USART_ISR_TXE) == USART_ISR_TXE) {
- if (fifo_empty(&output_fifo)) {
- USART1->CR1 &= ~(USART_CR1_TXEIE);
- }
- else {
- USART1->TDR = fifo_remove(&output_fifo);
- }
- }
- //-----------------------------------------------------------------
- // Leave this checking code here to make sure nothing bad happens.
- if (USART1->ISR & (USART_ISR_RXNE|
- USART_ISR_ORE|USART_ISR_NE|USART_ISR_FE|USART_ISR_PE)) {
- // safe_printf("Problem in USART1_IRQHandler: ISR = 0x%x\n", USART1->ISR);
- }
- }
- // See lab document for description
- static int getchar_irq(void) {
- // Student code goes here...
- // while(!fifo_newline(&input_fifo)) {
- // asm("wfi");
- // }
- return fifo_remove(&input_fifo);
- }
- // See lab document for description
- static int putchar_irq(char ch) {
- // Student code goes here...
- while (fifo_full(&output_fifo)) {
- asm("wfi");
- }
- if (ch == 10) {
- fifo_insert(&output_fifo, '\r');
- }
- else {
- fifo_insert(&output_fifo, ch);
- }
- if ((USART1->CR1 & USART_CR1_TXEIE) != USART_CR1_TXEIE) {
- USART1->CR1 |= USART_CR1_TXEIE;
- USART1_IRQHandler();
- }
- if (ch == 10) {
- while (fifo_full(&output_fifo)) {
- asm("wfi");
- }
- fifo_insert(&output_fifo, '\n');
- }
- }
- //=======================================================================
- // Called by the Standard Peripheral library for a write()
- int __io_putchar(int ch) {
- if (interrupt_mode == INTR)
- return putchar_irq(ch);
- }
- //=======================================================================
- // Called by the Standard Peripheral library for a read()
- int __io_getchar(void) {
- // Choose the right implementation.
- if (interrupt_mode == INTR)
- return getchar_irq();
- }
- void initilize_ships() {
- for (int i =0; i < 16; i++) {
- for (int j =0; j < 16; j++) {
- ships[i][j] = 0;
- }
- }
- for (int i =0; i <2; i++) {
- ships[0][i] = 1;
- }
- for (int i =0; i <3; i++) {
- ships[i+10][5] = 1;
- }
- for (int i =0; i <4; i++) {
- ships[i][9] = 1;
- ships[i][14] = 1;
- }
- for (int i =0; i <5; i++) {
- ships[3][i+2] = 1;
- }
- ships[3][14] = 0;
- return;
- }
- //void TIM3_IRQHandler() {
- //}
- /*void game() {
- if (player_1 == 1) { //this means stm 1 is going to be the first one going
- for (int i=0; i < 4;i++) {
- }
- }
- }
- */
- int col = 0;
- int row = -1;
- char char_array[4][4] = { {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'} };
- int int_array[4][4] = { {1,2,3,0xa},
- {4,5,6,0xb},
- {7,8,9,0xc},
- {0xf, 0, 0xe, 0xd} };
- uint8_t key_samples[4][4] = { {0}, {0}, {0}, {0} };
- uint8_t key_pressed[4][4] = { {0}, {0}, {0}, {0} };
- uint8_t key_released[4][4] = { {0}, {0}, {0}, {0} };
- void pb9out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER9);
- GPIOB->MODER |= GPIO_MODER_MODER9_0;
- if ((GPIOB->ODR & GPIO_ODR_9) == 0) {
- GPIOB->ODR |= GPIO_ODR_9;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_9;
- }
- }
- void pb8out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER8);
- GPIOB->MODER |= GPIO_MODER_MODER8_0;
- if ((GPIOB->ODR & GPIO_ODR_8) == 0) {
- GPIOB->ODR |= GPIO_ODR_8;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_8;
- }
- }
- void pb7out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER7);
- GPIOB->MODER |= GPIO_MODER_MODER7_0;
- if ((GPIOB->ODR & GPIO_ODR_7) == 0) {
- GPIOB->ODR |= GPIO_ODR_7;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_7;
- }
- }
- void pb6out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER6);
- GPIOB->MODER |= GPIO_MODER_MODER6_0;
- if ((GPIOB->ODR & GPIO_ODR_6) == 0) {
- GPIOB->ODR |= GPIO_ODR_6;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_6;
- }
- }
- void pb5out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER5);
- GPIOB->MODER |= GPIO_MODER_MODER5_0;
- if ((GPIOB->ODR & GPIO_ODR_5) == 0) {
- GPIOB->ODR |= GPIO_ODR_5;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_5;
- }
- }
- void pb4out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER4);
- GPIOB->MODER |= GPIO_MODER_MODER4_0;
- if ((GPIOB->ODR & GPIO_ODR_4) == 0) {
- GPIOB->ODR |= GPIO_ODR_4;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_4;
- }
- }
- void pb3out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER3);
- GPIOB->MODER |= GPIO_MODER_MODER3_0;
- if ((GPIOB->ODR & GPIO_ODR_3) == 0) {
- GPIOB->ODR |= GPIO_ODR_3;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_3;
- }
- }
- void pc12out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- GPIOC->MODER &= ~(GPIO_MODER_MODER12);
- GPIOC->MODER |= GPIO_MODER_MODER12_0;
- if ((GPIOC->ODR & GPIO_ODR_12) == 0) {
- GPIOC->ODR |= GPIO_ODR_12;
- }
- else {
- GPIOC->BSRR |= GPIO_BSRR_BR_12;
- }
- }
- void pc11out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- GPIOC->MODER &= ~(GPIO_MODER_MODER11);
- GPIOC->MODER |= GPIO_MODER_MODER11_0;
- if ((GPIOC->ODR & GPIO_ODR_11) == 0) {
- GPIOC->ODR |= GPIO_ODR_11;
- }
- else {
- GPIOC->BSRR |= GPIO_BSRR_BR_11;
- }
- }
- void update_key_press() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if ((key_samples[i][j] & KEY_PRESS_MASK) == 0b00000111) {
- key_pressed[i][j] = 1;
- key_samples[i][j] = 0xFF;
- }
- if ((key_samples[i][j] & KEY_REL_MASK) == 0b11100000) {
- key_released[i][j] = 1;
- key_samples[i][j] = 0x00;
- }
- }
- }
- }
- char get_char_key() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
- key_released[i][j] = 0;
- key_pressed[i][j] = 0;
- return char_array[i][j];
- }
- }
- }
- return '\0';
- }
- int get_key_pressed() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
- key_released[i][j] = 0;
- key_pressed[i][j] = 0;
- return int_array[i][j];
- }
- }
- }
- return -1;
- }
- void update_samples(int row) {
- // Update the current column with new values
- for(int i = 0; i < 4; i++) {
- uint8_t curr_history = key_samples[i][col];
- curr_history = curr_history << 1;
- if(row == i)
- curr_history |= 1;
- key_samples[i][col] = curr_history;
- }
- }
- void init_keypad() {
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER &= ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER1 |
- GPIO_MODER_MODER2 | GPIO_MODER_MODER3 |
- GPIO_MODER_MODER6 | GPIO_MODER_MODER7 |
- GPIO_MODER_MODER8 | GPIO_MODER_MODER9);
- // Columns set to output:
- GPIOA->MODER |= GPIO_MODER_MODER0_0 | GPIO_MODER_MODER1_0 |
- GPIO_MODER_MODER2_0 | GPIO_MODER_MODER3_0;
- // Pull-down resistors for rows:
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR4 | GPIO_PUPDR_PUPDR5 |
- GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7);
- GPIOA->PUPDR |= GPIO_PUPDR_PUPDR4_1 | GPIO_PUPDR_PUPDR5_1 |
- GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1;
- }
- void setup_timer3() {
- RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
- TIM3->PSC = 480 - 1;
- TIM3->ARR = 100 - 1;
- TIM3->DIER |= 0x1;
- NVIC -> ISER[0] = 1 << TIM3_IRQn;
- TIM3->CR1 |= TIM_CR1_CEN;
- }
- char key_scan() {
- int current_row = -1;
- if (((GPIOA->IDR >> 4) & 0x1) == 0x1) {
- current_row = 0;
- }
- else if (((GPIOA->IDR >> 5) & 0x1) == 0x1) {
- current_row = 1;
- }
- else if (((GPIOA->IDR >> 6) & 0x1) == 0x1) {
- current_row = 2;
- }
- else if (((GPIOA->IDR >> 7) & 0x1) == 0x1) {
- current_row = 3;
- }
- update_samples(current_row);
- update_key_press();
- col = (col + 1) % 4;
- GPIOA->ODR = 1<<col;
- return get_char_key();
- }
- void ack_tim3() {
- if ((TIM3->SR & TIM_SR_UIF) != 0) {
- TIM3->SR &= ~TIM_SR_UIF;
- }
- }
- void reset_grid() {
- for (int i = 0; i < 16; i++) {
- for (int j = 0; j < 16; j++) {
- grid[i][j] = 0;
- }
- }
- }
- void phase1(char key) {
- char input = getchar_irq();
- if (input == 'G') {
- my_turn = 'N';
- }
- if (key == '*') {
- my_turn = 'Y';
- phase = 2; //Change back to 2 later
- reset_grid();
- putchar_irq('G');
- }
- else if (my_turn == 'N') {
- phase = 2; // Change back to 2 in real
- reset_grid();
- }
- }
- void input_reset() {
- if (phase == 2) {
- for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++){
- phase2_input[i] = '\0';
- }
- }
- else {
- for(int i = 0; i < (sizeof phase3_input / sizeof phase3_input[0]); i++){
- phase3_input[i] = '\0';
- }
- }
- }
- int get_row(char key1, char key2) {
- int row_calc = 0;
- if (key1 == '1') {
- row_calc += 10;
- }
- row_calc += (key2 - 48);
- return row_calc;
- }
- int get_col(char key3, char key4) {
- int col_calc = 0;
- if (key3 == '1') {
- col_calc += 10;
- }
- col_calc += (key4 - 48);
- return col_calc;
- }
- bool phase2_check_input() {
- char key1 = phase2_input[0];
- char key2 = phase2_input[1];
- char key3 = phase2_input[2];
- char key4 = phase2_input[3];
- char key5 = phase2_input[4];
- char key6 = phase2_input[5];
- // char key7 = phase2_input[6];
- if ((key1 != 'A') && (key1 != 'B')) {
- return false;
- }
- else {
- if ((key2 - 48 < 2) || (key2 - 48 > 5)) {
- return false;
- }
- }
- if ((key3 != '0') && (key3 != '1')) {
- return false;
- }
- else if (key3 == '0') {
- if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
- return false;
- }
- }
- else if (key3 == '1') {
- if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
- return false;
- }
- }
- if ((key5 != '0') && (key5 != '1')) {
- return false;
- }
- else if (key5 == '0') {
- if ((key6 - 48 <= 0) || (key6 - 48 >= 10)) {
- return false;
- }
- }
- else if (key5 == '1') {
- if ((key6 - 48 < 0) || (key6 - 48 > 6)) {
- return false;
- }
- }
- /* if (key7 != '#') {
- return false;
- } */
- return true;
- }
- void phase2_full_reset() {
- reset_grid();
- input_index = 0;
- // display_phase2(0, 0);
- for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++) {
- phase2_input[i] = '\0';
- }
- ship5_set = false;
- ship4_set = false;
- ship2_set = false;
- ship3_1_set = false;
- ship3_2_set = false;
- }
- bool phase2_conflicts() { // Overlap & Limit-Over Check
- char key1 = phase2_input[0];
- char key2 = phase2_input[1];
- char key3 = phase2_input[2];
- char key4 = phase2_input[3];
- char key5 = phase2_input[4];
- char key6 = phase2_input[5];
- int row_calc = get_row(key3, key4) - 1;
- int col_calc = get_col(key5, key6) - 1;
- if (key1 == 'A') {
- if ((col_calc + (key2 - 48) - 1) > 16) {
- return true;
- }
- for (int j = (col_calc); j < (col_calc + key2 - 48); j++) {
- if (grid[row_calc][j] == 1) {
- return true;
- }
- }
- }
- else {
- if ((row_calc + (key2 - 48) - 1) > 16) {
- return true;
- }
- for (int i = (row_calc); i < (row_calc + key2 - 48); i++) {
- if (grid[i][col_calc] == 1) {
- return true;
- }
- }
- }
- return false;
- }
- bool phase2_used_ships() {
- if ((phase2_input[1] == '5') && (ship5_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '4') && (ship4_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '2') && (ship2_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '3') && (ship3_1_set == true) &&
- (ship3_2_set == true)){
- return true;
- }
- return false;
- }
- void phase2_set_ships() {
- char key1 = phase2_input[0];
- char key2 = phase2_input[1];
- char key3 = phase2_input[2];
- char key4 = phase2_input[3];
- char key5 = phase2_input[4];
- char key6 = phase2_input[5];
- char row_array[3] = {key3,key4,'\0'};
- char col_array[3] = {key5,key6,'\0'};
- int row_calc = atoi(row_array) - 1;
- int col_calc = atoi(col_array) - 1;
- //int row_calc = get_row(key3, key4);
- //int col_calc = get_col(key5, key6);
- if (key1 == 'A') {
- for (int j = col_calc; j < (col_calc + key2 - 48); j++) {
- grid[row_calc][j] = 1;
- // display_phase2(row_calc, j);
- }
- }
- else {
- for (int i = (row_calc); i < (row_calc + key2 - 48); i++) {
- grid[i][col_calc] = 1;
- // display_phase2(i, col_calc);
- }
- }
- }
- void phase2(char key) {
- if (key == 'C') {
- input_index = 0;
- input_reset();
- return;
- }
- else if (key == 'D') {
- phase2_full_reset();
- return;
- }
- if ((key == '*') || (key == '#')) {
- return;
- }
- if (input_index == 0) {
- input_reset();
- phase2_input[input_index] = key;
- }
- else if (input_index < 5) {
- phase2_input[input_index] = key;
- }
- else {
- phase2_input[input_index] = key;
- if (phase2_check_input() == true) {
- if ((phase2_conflicts() || phase2_used_ships()) == true) {
- input_reset();
- input_index = 0;
- return;
- }
- if (phase2_input[1] == '5') {
- ship5_set = true;
- phase2_set_ships();
- // pb9out();
- }
- else if (phase2_input[1] == '4') {
- ship4_set = true;
- phase2_set_ships();
- // pb8out();
- }
- else if (phase2_input[1] == '3') {
- if (ship3_1_set == false) {
- ship3_1_set = true;
- phase2_set_ships();
- pb7out();
- }
- else {
- if (ship3_2_set == false) {
- ship3_2_set = true;
- phase2_set_ships();
- pb6out();
- }
- else {
- input_reset();
- return;
- }
- }
- }
- else {
- ship2_set = true;
- phase2_set_ships();
- pb5out();
- }
- }
- else {
- input_reset();
- return;
- }
- if ((ship5_set && ship4_set && ship3_1_set && ship3_2_set && ship2_set)
- == true) {
- putchar_irq('O');
- phase = 3;
- }
- }
- input_index = (input_index + 1) % 6;
- }
- bool phase3_check_input() {
- // char key1 = phase3_input[0];
- // char key2 = phase3_input[1];
- // char key3 = phase3_input[2];
- // char key4 = phase3_input[3];
- // char key5 = phase3_input[4];
- char key1 = coord[0];
- char key2 = coord[1];
- char key3 = coord[2];
- char key4 = coord[3];
- // char key5 = phase3_input[4];
- if ((key1 != '0') && (key1 != '1')) {
- return false;
- }
- else if (key1 == '0') {
- if ((key2 - 48 <= 0) || (key2 - 48 >= 10)) {
- return false;
- }
- }
- else if (key1 == '1') {
- if ((key2 - 48 < 0) || (key2 - 48 > 6)) {
- return false;
- }
- }
- if ((key3 != '0') && (key3 != '1')) {
- return false;
- }
- else if (key3 == '0') {
- if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
- return false;
- }
- }
- else if (key3 == '1') {
- if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
- return false;
- }
- }
- // if (key5 != '#') {
- // return false;
- // }
- return true;
- }
- void phase3_clear() {
- t_i = 0;
- for (int i = 0; i < 4; i++) {
- coord[i] = '\0';
- }
- }
- void phase3(char key) {
- if (hit_opp == 17) { // WIN
- win = true;
- phase = 4;
- return;
- }
- else if (opp_hit == 17) { // LOSE
- phase = 4;
- return;
- }
- char result = getchar_irq();
- if ( result == 'L') {
- win = true;
- phase = 4;
- return;
- }
- if (my_turn == 'Y') {
- if ((key - 48 >= 0) && (key - 48 <= 9)) {
- coord[t_i] = key;
- t_i++;
- if (t_i == 4) {
- if (phase3_check_input() == true) {
- for (int i = 0; i < 4; i++) {
- putchar_irq(coord[i]);
- }
- my_turn = 'W';
- pb7out();
- }
- else {
- phase3_clear();
- }
- }
- t_i = t_i % 4;
- }
- else if ((key == 'C')) {
- phase3_clear();
- }
- else if (key == 'D') {
- putchar_irq('L');
- phase = 4;
- return;
- }
- }
- else if (my_turn == 'W') {
- if (result == 'H') {
- pc12out();
- hit_opp++;
- my_turn = 'N';
- }
- else if (result == 'M') {
- pb3out();
- my_turn = 'N';
- }
- }
- else if (my_turn == 'N') {
- if ((result - 48 >= 0) && (result - 48 <= 9)) {
- coord[t_i] = result;
- t_i = (t_i + 1) % 4;
- if (t_i == 0) {
- char tempx[3];
- char tempy[3];
- tempx[2] = '\0';
- tempx[0] = coord[0];
- tempx[1] = coord[1];
- tempy[2] = '\0';
- tempy[0] = coord[2];
- tempy[1] = coord[3];
- int x = atoi(tempx);
- int y = atoi(tempy);
- if (grid[x-1][y-1] == 1) {
- grid[x-1][y-1] = 0;
- pc11out();
- putchar_irq('H');
- opp_hit++;
- }
- else {
- putchar_irq('M');
- }
- my_turn = 'Y';
- }
- }
- }
- }
- void TIM3_IRQHandler() {
- char key = key_scan();
- if (phase == 1) {
- phase1(key);
- }
- else if (phase == 2) {
- if (key != '\0') {
- phase2(key);
- }
- }
- else if (phase == 3) {
- if (opponent_ready != 'Y') {
- char ready = getchar_irq();
- if (ready == 'O') {
- opponent_ready = 'Y';
- }
- }
- else {
- phase3(key);
- }
- }
- else {
- if (win == true) {
- // DISPLAY WIN
- printf("win");
- pb8out();
- }
- else {
- // DISPLAY LOSE
- printf("lose");
- pb9out();
- }
- }
- ack_tim3();
- }
- void main() {
- //printf("hhh");
- init_keypad();
- setup_timer3();
- phase = 1;
- // opponent_ready = 'Y';
- tty_init();
- enable_tty_irq();
- //initilize_ships();
- // counter = 0;
- // printf(getchar_irq());
- // printf("hey2!");
- //insert_echo_char('h');
- //insert_echo_char('e');
- //insert_echo_char('l');
- //insert_echo_char('l');
- //insert_echo_char('o');
- // getchar_irq();
- // for (int i=0; i < 5; i++) {
- // putchar_irq(getchar_irq());
- // }
- // putchar_irq('g');
- for(;;)
- asm("wfi");
- //TEST DELETE WHEN RUNNING GAME
- // DISPLAY/INIT START SCREEN
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement