![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This is THE 447 FINAL PROJECT this is the frame work put your code in the spot where the it suppose to go and. Make sure you import this into your complier and work on your section, once you done just commit the changes and fork to a new folder
Dependencies: mbed-rtos mbed draw_test EALib SWSPI
main.cpp@6:0f60a4070f09, 2015-12-05 (annotated)
- Committer:
- AndyTran
- Date:
- Sat Dec 05 10:24:47 2015 +0000
- Revision:
- 6:0f60a4070f09
- Parent:
- 4:b192d0d47309
- Child:
- 7:f7c3dc874db2
joystick work, reset work, initialize work, spi_between board is not work, losser check is not work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndyTran | 6:0f60a4070f09 | 1 | #include <stdio.h> |
AndyTran | 6:0f60a4070f09 | 2 | #include <stdlib.h> |
AndyTran | 2:b14759e704c7 | 3 | #include <iostream> |
AndyTran | 6:0f60a4070f09 | 4 | #include <time.h> |
AndyTran | 0:4abb197a773c | 5 | #include "mbed.h" |
AndyTran | 0:4abb197a773c | 6 | #include "cmsis_os.h" |
AndyTran | 6:0f60a4070f09 | 7 | #include "MMA7455.h" |
AndyTran | 4:b192d0d47309 | 8 | #include "game_board.h" //duck soup |
AndyTran | 6:0f60a4070f09 | 9 | |
AndyTran | 2:b14759e704c7 | 10 | using namespace std; |
AndyTran | 6:0f60a4070f09 | 11 | game_board battle; |
AndyTran | 6:0f60a4070f09 | 12 | RawSerial pc(USBTX, USBRX); // uart |
AndyTran | 6:0f60a4070f09 | 13 | MMA7455 accSensor (P0_27, P0_28); //acceleronmeter i2c |
AndyTran | 6:0f60a4070f09 | 14 | SPI LEDs(p5, NC, p7); // LEDs driver spi |
AndyTran | 6:0f60a4070f09 | 15 | DigitalOut cs (p30); |
AndyTran | 6:0f60a4070f09 | 16 | AnalogIn trimpot(p15);//might need to change the trimpot location if conflict with spi |
AndyTran | 6:0f60a4070f09 | 17 | //SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) //check mapping for pin |
AndyTran | 6:0f60a4070f09 | 18 | //SPISlave (mosi, miso, sclk, ssel) //check mapping for pin |
AndyTran | 0:4abb197a773c | 19 | |
AndyTran | 6:0f60a4070f09 | 20 | //joystick |
AndyTran | 6:0f60a4070f09 | 21 | DigitalIn Up(p32); |
AndyTran | 6:0f60a4070f09 | 22 | DigitalIn Down(p38); |
AndyTran | 6:0f60a4070f09 | 23 | DigitalIn Left(p39); |
AndyTran | 6:0f60a4070f09 | 24 | DigitalIn Right(p37); |
AndyTran | 6:0f60a4070f09 | 25 | DigitalIn Center(p31); |
AndyTran | 6:0f60a4070f09 | 26 | //joystick done |
AndyTran | 6:0f60a4070f09 | 27 | |
AndyTran | 6:0f60a4070f09 | 28 | //RGB LED |
AndyTran | 6:0f60a4070f09 | 29 | DigitalOut led1(p25); //red |
AndyTran | 6:0f60a4070f09 | 30 | DigitalOut led2(p26);//green |
AndyTran | 6:0f60a4070f09 | 31 | DigitalOut led3(p28);//blue |
AndyTran | 6:0f60a4070f09 | 32 | //RGB LED |
AndyTran | 6:0f60a4070f09 | 33 | int initlize = 0; |
AndyTran | 0:4abb197a773c | 34 | //declare your input pin and output pin here: |
AndyTran | 0:4abb197a773c | 35 | //DigitalOut led1(p25); |
AndyTran | 0:4abb197a773c | 36 | //DigitalOut led2(p26); |
AndyTran | 0:4abb197a773c | 37 | //DigitalOut led3(p28); |
AndyTran | 0:4abb197a773c | 38 | // |
AndyTran | 0:4abb197a773c | 39 | // declare you globle variable here: |
AndyTran | 6:0f60a4070f09 | 40 | unsigned char my_X_coor = 1, my_Y_coor = 1; |
AndyTran | 6:0f60a4070f09 | 41 | uint8_t my_LEDs = 0x1A; |
AndyTran | 6:0f60a4070f09 | 42 | uint8_t SPI_X_send = 0x01,SPI_Y_send = 0x01; |
AndyTran | 2:b14759e704c7 | 43 | unsigned char other_X_coor = 0, other_Y_coor = 0; |
AndyTran | 2:b14759e704c7 | 44 | int friendly [6][6]; // array to determine friendly location |
AndyTran | 6:0f60a4070f09 | 45 | int winner; |
AndyTran | 6:0f60a4070f09 | 46 | int A_x_old = 0,A_y_old = 0,A_z_old = 0; |
AndyTran | 0:4abb197a773c | 47 | //X_coor is x coordinate from 0 to 6 |
AndyTran | 0:4abb197a773c | 48 | //Y_coor is y coordinate form A to F |
AndyTran | 0:4abb197a773c | 49 | // |
AndyTran | 0:4abb197a773c | 50 | // |
AndyTran | 0:4abb197a773c | 51 | // |
AndyTran | 6:0f60a4070f09 | 52 | void LEDsoutput(uint8_t value) { |
AndyTran | 6:0f60a4070f09 | 53 | cs = 0; |
AndyTran | 6:0f60a4070f09 | 54 | LEDs.write(value); |
AndyTran | 6:0f60a4070f09 | 55 | cs = 1; |
AndyTran | 6:0f60a4070f09 | 56 | } |
AndyTran | 0:4abb197a773c | 57 | // this thread is your SPI communication between 2 board |
AndyTran | 0:4abb197a773c | 58 | void SPI_communication(void const *args) { |
AndyTran | 2:b14759e704c7 | 59 | //send my_X_coor and my_Y_coor if the center button is hit |
AndyTran | 2:b14759e704c7 | 60 | //receive other board x_coor and y_coor |
AndyTran | 0:4abb197a773c | 61 | //spi code here |
AndyTran | 4:b192d0d47309 | 62 | //turn 1 :friendly |
AndyTran | 4:b192d0d47309 | 63 | // player1 send shooting location |
AndyTran | 4:b192d0d47309 | 64 | // player1 get shooting location and whether it hit or miss |
AndyTran | 4:b192d0d47309 | 65 | //turn 1 :enemy |
AndyTran | 4:b192d0d47309 | 66 | // get enemy shooting location |
AndyTran | 4:b192d0d47309 | 67 | // check wheather is it a hit or miss |
AndyTran | 4:b192d0d47309 | 68 | // echo enemy shooting location and hit/miss flag |
AndyTran | 4:b192d0d47309 | 69 | //send out winner status |
AndyTran | 4:b192d0d47309 | 70 | // 0 = still no winner |
AndyTran | 4:b192d0d47309 | 71 | // 1 = i am lose |
AndyTran | 4:b192d0d47309 | 72 | // so if you receive a 1 that mean you win |
AndyTran | 0:4abb197a773c | 73 | } |
AndyTran | 0:4abb197a773c | 74 | |
AndyTran | 0:4abb197a773c | 75 | // joystick update x_coor and y_coor here |
AndyTran | 0:4abb197a773c | 76 | void joy_stick_read(void const *args) { //might be an interrupt thread here for interrupt read of joystick |
AndyTran | 0:4abb197a773c | 77 | // joy stick to update global val of x_coor and y_coor; |
AndyTran | 2:b14759e704c7 | 78 | // convert y coor to hex offset |
AndyTran | 0:4abb197a773c | 79 | // update the 8 LEDs here |
AndyTran | 6:0f60a4070f09 | 80 | while (true) { |
AndyTran | 6:0f60a4070f09 | 81 | //pc.printf ("in joystick"); |
AndyTran | 6:0f60a4070f09 | 82 | if (Up.read() == 0) { |
AndyTran | 6:0f60a4070f09 | 83 | my_Y_coor = my_Y_coor + 1; |
AndyTran | 6:0f60a4070f09 | 84 | if (my_Y_coor == 7) { |
AndyTran | 6:0f60a4070f09 | 85 | my_Y_coor = 1; |
AndyTran | 6:0f60a4070f09 | 86 | } |
AndyTran | 6:0f60a4070f09 | 87 | } else if (Down.read() == 0) { |
AndyTran | 6:0f60a4070f09 | 88 | my_Y_coor = my_Y_coor - 1; |
AndyTran | 6:0f60a4070f09 | 89 | if (my_Y_coor == 0) { |
AndyTran | 6:0f60a4070f09 | 90 | my_Y_coor = 6; |
AndyTran | 6:0f60a4070f09 | 91 | } |
AndyTran | 6:0f60a4070f09 | 92 | } else if (Left.read() == 0) { |
AndyTran | 6:0f60a4070f09 | 93 | my_X_coor = my_X_coor + 1 ; |
AndyTran | 6:0f60a4070f09 | 94 | if (my_X_coor == 7) { |
AndyTran | 6:0f60a4070f09 | 95 | my_X_coor = 1; |
AndyTran | 6:0f60a4070f09 | 96 | } |
AndyTran | 6:0f60a4070f09 | 97 | //my_LEDs = my_X_coor; |
AndyTran | 6:0f60a4070f09 | 98 | } else if (Right.read() == 0) { |
AndyTran | 6:0f60a4070f09 | 99 | my_X_coor = my_X_coor - 1; |
AndyTran | 6:0f60a4070f09 | 100 | if (my_X_coor == 0) { |
AndyTran | 6:0f60a4070f09 | 101 | my_X_coor = 6; |
AndyTran | 6:0f60a4070f09 | 102 | } |
AndyTran | 6:0f60a4070f09 | 103 | //my_LEDs = my_X_coor; |
AndyTran | 6:0f60a4070f09 | 104 | } else { |
AndyTran | 6:0f60a4070f09 | 105 | my_X_coor = my_X_coor; |
AndyTran | 6:0f60a4070f09 | 106 | my_Y_coor = my_Y_coor; |
AndyTran | 6:0f60a4070f09 | 107 | } |
AndyTran | 6:0f60a4070f09 | 108 | my_LEDs = (my_X_coor << 4)|(0x0F-(my_Y_coor-1)); |
AndyTran | 6:0f60a4070f09 | 109 | LEDsoutput (my_LEDs); |
AndyTran | 6:0f60a4070f09 | 110 | SPI_X_send = my_X_coor; |
AndyTran | 6:0f60a4070f09 | 111 | SPI_Y_send = my_Y_coor; |
AndyTran | 6:0f60a4070f09 | 112 | if (Center.read() == 0) { |
AndyTran | 6:0f60a4070f09 | 113 | pc.printf ("got middle\n\r"); |
AndyTran | 6:0f60a4070f09 | 114 | } |
AndyTran | 6:0f60a4070f09 | 115 | osDelay (170); |
AndyTran | 6:0f60a4070f09 | 116 | } |
AndyTran | 6:0f60a4070f09 | 117 | //joystick is done and over with DO NOT touch |
AndyTran | 6:0f60a4070f09 | 118 | |
AndyTran | 0:4abb197a773c | 119 | } |
AndyTran | 0:4abb197a773c | 120 | |
AndyTran | 0:4abb197a773c | 121 | // this thread use to update the game board |
AndyTran | 2:b14759e704c7 | 122 | void game_view_update (void const *args) { |
AndyTran | 2:b14759e704c7 | 123 | // board of this game will be a class this update and get info to and from the class |
AndyTran | 0:4abb197a773c | 124 | // update the by led game update |
AndyTran | 1:3f7aa28da00d | 125 | // Uart runing at 921600 max speed; hopefully print 1 line and out; |
AndyTran | 4:b192d0d47309 | 126 | // using an interger then cycle through the get string function to put it inti the printf function |
AndyTran | 4:b192d0d47309 | 127 | |
AndyTran | 3:84d9362dffeb | 128 | // * = hit |
AndyTran | 1:3f7aa28da00d | 129 | // o = miss |
AndyTran | 1:3f7aa28da00d | 130 | // > = your ship ship |
AndyTran | 2:b14759e704c7 | 131 | |
AndyTran | 2:b14759e704c7 | 132 | // 1) check for a hit |
AndyTran | 2:b14759e704c7 | 133 | // 2) update the game board |
AndyTran | 2:b14759e704c7 | 134 | |
AndyTran | 1:3f7aa28da00d | 135 | // BLANK game Board |
AndyTran | 1:3f7aa28da00d | 136 | // F _ _ _ _ _ _ F _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 137 | // E _ _ _ _ _ _ E _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 138 | // D _ _ _ _ _ _ D _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 139 | // C _ _ _ _ _ _ C _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 140 | // B _ _ _ _ _ _ B _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 141 | // A _ _ _ _ _ _ A _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 142 | // 1 2 3 4 5 6 1 2 3 4 5 6 |
AndyTran | 1:3f7aa28da00d | 143 | // Friendly Enemy |
AndyTran | 1:3f7aa28da00d | 144 | |
AndyTran | 1:3f7aa28da00d | 145 | // Example of running game |
AndyTran | 1:3f7aa28da00d | 146 | // F _ O _ > _ _ F _ O _ _ _ _ |
AndyTran | 3:84d9362dffeb | 147 | // E > _ O _ > _ E _ _ * _ _ _ |
AndyTran | 1:3f7aa28da00d | 148 | // D _ _ > _ _ _ D _ _ _ O _ _ |
AndyTran | 1:3f7aa28da00d | 149 | // C O _ _ _ _ _ C _ O _ _ _ _ |
AndyTran | 3:84d9362dffeb | 150 | // B _ * _ O _ _ B _ _ _ * _ _ //X on friendly mean your ship got destroy |
AndyTran | 1:3f7aa28da00d | 151 | // A _ _ _ > _ _ A _ _ _ _ _ _ |
AndyTran | 1:3f7aa28da00d | 152 | // 1 2 3 4 5 6 1 2 3 4 5 6 |
AndyTran | 1:3f7aa28da00d | 153 | // Friendly Enemy |
AndyTran | 1:3f7aa28da00d | 154 | // Turn 1 = miss at 2F |
AndyTran | 2:b14759e704c7 | 155 | // Turn 2 = We sink an enemy battle ship at 3E |
AndyTran | 1:3f7aa28da00d | 156 | // Turn 3 = miss at 4D |
AndyTran | 1:3f7aa28da00d | 157 | // Turn 4 = miss at 2C |
AndyTran | 2:b14759e704c7 | 158 | // Tunr 5 = We sink an enemy battle ship at 4B |
AndyTran | 1:3f7aa28da00d | 159 | |
AndyTran | 0:4abb197a773c | 160 | } |
AndyTran | 0:4abb197a773c | 161 | |
AndyTran | 0:4abb197a773c | 162 | // code here to check for winer and turn on the RGB |
AndyTran | 2:b14759e704c7 | 163 | void winnercheck (void const *args) { |
AndyTran | 4:b192d0d47309 | 164 | //check for winner by destroy all enemy ship; |
AndyTran | 6:0f60a4070f09 | 165 | bool gamelost; |
AndyTran | 6:0f60a4070f09 | 166 | gamelost = battle.looser(); |
AndyTran | 6:0f60a4070f09 | 167 | if (gamelost == 1) |
AndyTran | 6:0f60a4070f09 | 168 | { |
AndyTran | 6:0f60a4070f09 | 169 | //this board is loser |
AndyTran | 6:0f60a4070f09 | 170 | //send a message to the winner |
AndyTran | 6:0f60a4070f09 | 171 | } |
AndyTran | 6:0f60a4070f09 | 172 | else if (winner == 1)//spi message is the winner) |
AndyTran | 6:0f60a4070f09 | 173 | { |
AndyTran | 6:0f60a4070f09 | 174 | //blink the RGB LED |
AndyTran | 6:0f60a4070f09 | 175 | // |
AndyTran | 6:0f60a4070f09 | 176 | } |
AndyTran | 6:0f60a4070f09 | 177 | led1 = 1; |
AndyTran | 6:0f60a4070f09 | 178 | led2 = 1; |
AndyTran | 6:0f60a4070f09 | 179 | led3 = 1; |
AndyTran | 6:0f60a4070f09 | 180 | |
AndyTran | 0:4abb197a773c | 181 | } |
AndyTran | 0:4abb197a773c | 182 | |
AndyTran | 0:4abb197a773c | 183 | //read the accele |
AndyTran | 6:0f60a4070f09 | 184 | void checkreset (void const *args) |
AndyTran | 6:0f60a4070f09 | 185 | { |
AndyTran | 6:0f60a4070f09 | 186 | |
AndyTran | 0:4abb197a773c | 187 | //read the acceleron meter and compare against the previous value to |
AndyTran | 0:4abb197a773c | 188 | //determine if the board have been shake the reset the game board; |
AndyTran | 6:0f60a4070f09 | 189 | //the determinant is the x axis |
AndyTran | 6:0f60a4070f09 | 190 | while (true) { |
AndyTran | 6:0f60a4070f09 | 191 | |
AndyTran | 6:0f60a4070f09 | 192 | int Ax,Ay,Az; |
AndyTran | 6:0f60a4070f09 | 193 | int x_old = 0, y_old = 0; |
AndyTran | 6:0f60a4070f09 | 194 | int x, y; |
AndyTran | 6:0f60a4070f09 | 195 | int i; |
AndyTran | 6:0f60a4070f09 | 196 | int seed = trimpot.read()*1000; |
AndyTran | 6:0f60a4070f09 | 197 | srand (seed); |
AndyTran | 6:0f60a4070f09 | 198 | if(! accSensor.read(Ax,Ay,Az)) {}; |
AndyTran | 6:0f60a4070f09 | 199 | if (abs(Ax-A_x_old) > 20) { |
AndyTran | 6:0f60a4070f09 | 200 | pc.printf("new game \n\r"); |
AndyTran | 6:0f60a4070f09 | 201 | battle.new_game_board(); |
AndyTran | 6:0f60a4070f09 | 202 | for (i = 1; i < 9; i++) { |
AndyTran | 6:0f60a4070f09 | 203 | string a; |
AndyTran | 6:0f60a4070f09 | 204 | a = battle.get1row(i); |
AndyTran | 6:0f60a4070f09 | 205 | pc.printf ("%s", a); |
AndyTran | 6:0f60a4070f09 | 206 | |
AndyTran | 6:0f60a4070f09 | 207 | } |
AndyTran | 6:0f60a4070f09 | 208 | //randomize ship position 6 ship total |
AndyTran | 6:0f60a4070f09 | 209 | for (i = 0; i < 7; i++) { |
AndyTran | 6:0f60a4070f09 | 210 | x = (rand()%6 +1); |
AndyTran | 6:0f60a4070f09 | 211 | while (x == x_old) { |
AndyTran | 6:0f60a4070f09 | 212 | x = (rand()%6 +1); |
AndyTran | 6:0f60a4070f09 | 213 | } |
AndyTran | 6:0f60a4070f09 | 214 | x_old = x; |
AndyTran | 6:0f60a4070f09 | 215 | y = (rand()%5 +1); |
AndyTran | 6:0f60a4070f09 | 216 | while (y == y_old) { |
AndyTran | 6:0f60a4070f09 | 217 | y = (rand()%5 +1); |
AndyTran | 6:0f60a4070f09 | 218 | } |
AndyTran | 6:0f60a4070f09 | 219 | y_old = y; |
AndyTran | 6:0f60a4070f09 | 220 | battle.place_ship(x,y); |
AndyTran | 6:0f60a4070f09 | 221 | } |
AndyTran | 6:0f60a4070f09 | 222 | for (i = 1; i < 9; i++) { |
AndyTran | 6:0f60a4070f09 | 223 | string a; |
AndyTran | 6:0f60a4070f09 | 224 | a = battle.get1row(i); |
AndyTran | 6:0f60a4070f09 | 225 | pc.printf ("%s", a); |
AndyTran | 6:0f60a4070f09 | 226 | } |
AndyTran | 6:0f60a4070f09 | 227 | Ax = 0; |
AndyTran | 6:0f60a4070f09 | 228 | A_x_old = 0; |
AndyTran | 6:0f60a4070f09 | 229 | } else |
AndyTran | 6:0f60a4070f09 | 230 | A_x_old = Ax; |
AndyTran | 6:0f60a4070f09 | 231 | osDelay(500); |
AndyTran | 6:0f60a4070f09 | 232 | } |
AndyTran | 6:0f60a4070f09 | 233 | //reset is done and working do not touch |
AndyTran | 0:4abb197a773c | 234 | } |
AndyTran | 0:4abb197a773c | 235 | |
AndyTran | 0:4abb197a773c | 236 | |
AndyTran | 2:b14759e704c7 | 237 | osThreadDef(SPI_communication, osPriorityNormal, DEFAULT_STACK_SIZE); //comm between 2 board define thread |
AndyTran | 2:b14759e704c7 | 238 | osThreadDef(joy_stick_read, osPriorityNormal, DEFAULT_STACK_SIZE);//might be an interrupt define thread |
AndyTran | 2:b14759e704c7 | 239 | osThreadDef(game_view_update, osPriorityNormal, DEFAULT_STACK_SIZE);//update game view write to the fifo uart buffer define thread |
AndyTran | 2:b14759e704c7 | 240 | osThreadDef(winnercheck, osPriorityNormal, DEFAULT_STACK_SIZE); //check for winner define thread |
AndyTran | 2:b14759e704c7 | 241 | osThreadDef(checkreset, osPriorityNormal, DEFAULT_STACK_SIZE); //check for reset define thread |
AndyTran | 0:4abb197a773c | 242 | |
AndyTran | 0:4abb197a773c | 243 | int main() { |
AndyTran | 0:4abb197a773c | 244 | osThreadCreate(osThread(SPI_communication), NULL); |
AndyTran | 6:0f60a4070f09 | 245 | osThreadCreate(osThread(joy_stick_read), NULL); //done |
AndyTran | 0:4abb197a773c | 246 | osThreadCreate(osThread(game_view_update), NULL); |
AndyTran | 0:4abb197a773c | 247 | osThreadCreate(osThread(winnercheck), NULL); |
AndyTran | 6:0f60a4070f09 | 248 | osThreadCreate(osThread(checkreset), NULL);//done |
AndyTran | 0:4abb197a773c | 249 | // initialize all peripheral here |
AndyTran | 0:4abb197a773c | 250 | // |
AndyTran | 0:4abb197a773c | 251 | // |
AndyTran | 0:4abb197a773c | 252 | // |
AndyTran | 4:b192d0d47309 | 253 | // create a initialize thread with the key |
AndyTran | 4:b192d0d47309 | 254 | // if key is 0 run the initialize the change key to 1 |
AndyTran | 4:b192d0d47309 | 255 | // if key is 1 then get do nothing and get out |
AndyTran | 0:4abb197a773c | 256 | |
AndyTran | 2:b14759e704c7 | 257 | //note game initialize and setup upon shake the board |
AndyTran | 2:b14759e704c7 | 258 | |
AndyTran | 6:0f60a4070f09 | 259 | if (initlize == 0) |
AndyTran | 6:0f60a4070f09 | 260 | { |
AndyTran | 6:0f60a4070f09 | 261 | int x_old = 0, y_old = 0; |
AndyTran | 6:0f60a4070f09 | 262 | int x, y; |
AndyTran | 6:0f60a4070f09 | 263 | int i; |
AndyTran | 6:0f60a4070f09 | 264 | int seed = trimpot.read()*1000; |
AndyTran | 6:0f60a4070f09 | 265 | srand (seed); |
AndyTran | 6:0f60a4070f09 | 266 | pc.baud(921600); |
AndyTran | 6:0f60a4070f09 | 267 | pc.printf("Battle Ship by Andy, Adam, Loc, Ivan\n\r"); |
AndyTran | 6:0f60a4070f09 | 268 | while(!accSensor.setMode(MMA7455::ModeMeasurement)) { |
AndyTran | 6:0f60a4070f09 | 269 | pc.printf("Unable to set measurement mode.\n\r"); |
AndyTran | 6:0f60a4070f09 | 270 | } |
AndyTran | 6:0f60a4070f09 | 271 | while(!accSensor.setRange(MMA7455::Range_8g)) { |
AndyTran | 6:0f60a4070f09 | 272 | pc.printf("Unable to set the range.\n\r"); |
AndyTran | 6:0f60a4070f09 | 273 | } |
AndyTran | 6:0f60a4070f09 | 274 | while(!accSensor.calibrate()) { |
AndyTran | 6:0f60a4070f09 | 275 | pc.printf("Unable to calibrate\n\r"); |
AndyTran | 6:0f60a4070f09 | 276 | } |
AndyTran | 6:0f60a4070f09 | 277 | battle.new_game_board(); |
AndyTran | 6:0f60a4070f09 | 278 | for (i = 1; i < 9; i++){ |
AndyTran | 6:0f60a4070f09 | 279 | string a; |
AndyTran | 6:0f60a4070f09 | 280 | a = battle.get1row(i); |
AndyTran | 6:0f60a4070f09 | 281 | pc.printf ("%s", a); |
AndyTran | 6:0f60a4070f09 | 282 | |
AndyTran | 6:0f60a4070f09 | 283 | } |
AndyTran | 6:0f60a4070f09 | 284 | //randomize ship position 6 ship total |
AndyTran | 6:0f60a4070f09 | 285 | for (i = 0; i < 7; i++) { |
AndyTran | 6:0f60a4070f09 | 286 | x = (rand()%6 +1); |
AndyTran | 6:0f60a4070f09 | 287 | while (x == x_old) |
AndyTran | 6:0f60a4070f09 | 288 | { |
AndyTran | 6:0f60a4070f09 | 289 | x = (rand()%6 +1); |
AndyTran | 6:0f60a4070f09 | 290 | } |
AndyTran | 6:0f60a4070f09 | 291 | x_old = x; |
AndyTran | 6:0f60a4070f09 | 292 | y = (rand()%5 +1); |
AndyTran | 6:0f60a4070f09 | 293 | while (y == y_old) |
AndyTran | 6:0f60a4070f09 | 294 | { |
AndyTran | 6:0f60a4070f09 | 295 | y = (rand()%5 +1); |
AndyTran | 6:0f60a4070f09 | 296 | } |
AndyTran | 6:0f60a4070f09 | 297 | y_old = y; |
AndyTran | 6:0f60a4070f09 | 298 | battle.place_ship(x,y); |
AndyTran | 6:0f60a4070f09 | 299 | } |
AndyTran | 6:0f60a4070f09 | 300 | for (i = 1; i < 9; i++){ |
AndyTran | 6:0f60a4070f09 | 301 | string a; |
AndyTran | 6:0f60a4070f09 | 302 | a = battle.get1row(i); |
AndyTran | 6:0f60a4070f09 | 303 | pc.printf ("%s", a); |
AndyTran | 6:0f60a4070f09 | 304 | } |
AndyTran | 6:0f60a4070f09 | 305 | initlize = 1; |
AndyTran | 6:0f60a4070f09 | 306 | |
AndyTran | 6:0f60a4070f09 | 307 | } |
AndyTran | 0:4abb197a773c | 308 | //Main Thread |
AndyTran | 0:4abb197a773c | 309 | while (true) { |
AndyTran | 6:0f60a4070f09 | 310 | //osDelay (30); |
AndyTran | 6:0f60a4070f09 | 311 | //pc.printf ("in main"); |
AndyTran | 0:4abb197a773c | 312 | //check status and update status |
AndyTran | 0:4abb197a773c | 313 | } |
AndyTran | 0:4abb197a773c | 314 | } |