Jirat Nakarit
/
project_ShipDot
Jirat
Fork of project by
Diff: main.cpp
- Revision:
- 22:5bc894988f11
- Parent:
- 21:d76aa19984be
- Child:
- 23:66a401850f2e
diff -r d76aa19984be -r 5bc894988f11 main.cpp --- a/main.cpp Sat Dec 03 17:45:57 2016 +0000 +++ b/main.cpp Sun Dec 04 05:02:03 2016 +0000 @@ -2,12 +2,14 @@ /* This Nicleo is state 0 */ /******************************************************************************/ #include "mbed.h" +#include "ColorMbed.h" // Initail I/O. -AnalogIn VRx(A2); -AnalogIn VRy(A3); -DigitalIn Button(D13); -Serial bt(D8, D2); +AnalogIn VRx(A0); +AnalogIn VRy(A1); +DigitalIn Button(PA_4); +Serial bt(PA_15, PB_7); +ColorMbed dotmatrix; // Initial function. char posit2char(int posit); @@ -33,7 +35,7 @@ void print_atk_platform(); // Setting -int ifUrJirat = 1; +int ifUrJirat = 0; // Initial variable. int check_ship_underATK = 0; int select_Position = 0; @@ -56,6 +58,631 @@ int ship2[2][2] = {{0}}; int ship1[4] = {0}; +//fuction nasrun +int return_position[8][8] ; +int for_hex = 0; +int ch_post[8][8] = {{1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}, + {1,1,1,1,1,1,1,1}}; +int return_hex[8]; + +int display_battle[8][8][3] = {0}; + +int _temppic[8] = {0x01,0x01,0x01,0x01,0,0,0,0}; + +int temppic2[8] = {0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +int temppic3[3][8] = {{0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00},{0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}; +int temppic4[8] = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +int forshift[8] = {0}; +int pic[8] = {0}; + +int i,j; +int check_boat3=0; +int check_hex=0; + + +void hex_to_array(int* ar){ + int i; + int j; + int temp; + for(i = 0;i<8;i++){ + temp = ar[i]; + for(j = 0;j<8;j++){ + if(0x80 & temp){ + return_position[i][j] = 1; + } + else{ + return_position[i][j] = 0; + } + temp = temp << 1; + } + } +} + +void ch_same_position(int ar1[8][8],int ar2[8][8]){ + int i; + int j; + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + if(ar1[i][j] == 1 && ar2[i][j] == 1){ + ch_post[i][j] = 1; + } + else{ + ch_post[i][j] = 0; + } + } + } + +} + +void discard_same_position(int ar1[8][8],int ar2[8][8]){ + int i; + int j; + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + if(ar1[i][j] == ch_post[i][j] && ar2[i][j] == ch_post[i][j] ){ + ar1[i][j] = 0; + ar2[i][j] = 0; + } + } + } +} + + +int _power(int number , int power){ + int i = 0; + int number1 = number; + if(power == 0){return 1;} + for(i = 1 ; i <= power ; i++ ){ + if(power == 1){return number;} + if(i == 1){number = number * 1;} + else{number1 = number1 * number;} + } +return number1; +} + + +void array_to_hex(int ar1[8][8]){ + int prompt; + int i; + int j; + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + prompt = _power(2,7-j); + if(ar1[i][j] == 1){ + for_hex = for_hex + prompt; + } + else{ + for_hex = for_hex + 0; + } + } + return_hex[i] = for_hex; + prompt = 0; + for_hex = 0; + } +} + +void place_ship3(char dir){ + int post1[8][8] = {0}; + int post2[8][8] = {0}; + int hex_post1[8] = {0}; + int hex_post2[8] = {0}; + int hex_post_cut[8] = {0}; + int get_same_position[8][8]; + int wow=0; + + if(dir == 'w'&& temppic2[0] == 0){ + printf("w\n"); + for(j=0;j<8;j++){ + if(j == 7){ + forshift[7] = 0; + } + else{ + forshift[j] = temppic2[j+1]; + } + } + for(j=0;j<8;j++){ + temppic2[j] = forshift[j]; + _temppic[j] = temppic2[j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + } + if(dir == 'a'){ + printf("a\n"); + + for(i=0;i<8;i++){ + if(temppic2[i] == 0xe0){ + check_boat3 = 1; + } + } + if(check_boat3 == 0){ + for(j=0;j<8;j++){ + if(temppic2[j] != 0){ + temppic2[j] = temppic2[j]*2; + } + else{ + temppic2[j] = 0; + } + _temppic[j] = temppic2[j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + } + check_boat3 = 0; + } + if(dir == 'd'){ + printf("d\n"); + for(i=0;i<8;i++){ + if(temppic2[i] == 0x07){ + check_boat3 = 1; + } + } + if(check_boat3 == 0){ + for(j=0;j<8;j++){ + if(temppic2[j] != 0){ + temppic2[j] = temppic2[j]/2; + } + else{ + temppic2[j] = 0; + } + _temppic[j] = temppic2[j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + } + check_boat3 = 0; + } + if(dir == 's' && temppic2[7] == 0){ + printf("s\n"); + for(j=0;j<8;j++){ + if(j == 0){ + forshift[0] = 0; + } + else{ + forshift[j] = temppic2[j-1]; + } + } + for(j=0;j<8;j++){ + temppic2[j] = forshift[j]; + _temppic[j] = temppic2[j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic2[i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + + } +} +// ******************************************************************** ship 2 channal ****************************************************************** +void place_ship2_4(int ship,char dir){ + + int post1[8][8] = {0}; + int post2[8][8] = {0}; + int hex_post1[8] = {0}; + int hex_post2[8] = {0}; + int hex_post_cut[8] = {0}; + int get_same_position[8][8]; + int wow=0; + + if(dir == 'w'&& temppic3[ship][0] == 0){ + printf("w\n"); + for(j=0;j<8;j++){ + if(j == 7){ + forshift[7] = 0; + } + else{ + forshift[j] = temppic3[ship][j+1]; + } + } + for(j=0;j<8;j++){ + temppic3[ship][j] = forshift[j]; + _temppic[j] = temppic3[ship][j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + + } + if(dir == 'a' && temppic3[ship][3] != 0x80 && temppic3[ship][4] != 0x80 ){ + printf("a\n"); + for(j=0;j<8;j++){ + if(temppic3[j] != 0){ + temppic3[ship][j] = temppic3[ship][j]*2; + } + else{ + temppic3[ship][j] = 0; + } + _temppic[j] = temppic3[ship][j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + + } + if(dir == 'd' && temppic3[ship][3] != 0x01 && temppic3[ship][4] != 0x01 ){ + printf("d\n"); + for(j=0;j<8;j++){ + if(temppic3[ship][j] != 0){ + temppic3[ship][j] = temppic3[ship][j]/2; + } + else{ + temppic3[ship][j] = 0; + } + _temppic[j] = temppic3[ship][j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + + } + if(dir == 's' && temppic3[ship][7] == 0){ + printf("s\n"); + for(j=0;j<8;j++){ + if(j == 0){ + forshift[0] = 0; + } + else{ + forshift[j] = temppic3[ship][j-1]; + } + } + for(j=0;j<8;j++){ + temppic3[ship][j] = forshift[j]; + _temppic[j] = temppic3[ship][j]; + } + // begin to compare and combine two frame + hex_to_array(_temppic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post1[i][j] = return_position[i][j]; + } + } + hex_to_array(pic); + for(i = 0;i<8;i++){ + for(j = 0;j<8;j++){ + post2[i][j] = return_position[i][j]; + } + } + + // begin to check boypeacemaker + ch_same_position(post1,post2); + for(i=0;i<8;i++){ + for(j=0;j<8;j++){ + get_same_position[i][j] = ch_post[i][j]; + if(get_same_position[i][j] == 1){ + wow = 1; + } + } + } + if(wow == 0){ + for(i=0;i<8;i++){_temppic[i] = temppic3[ship][i]+pic[i];} + } + else{ + discard_same_position(post1,post2); + array_to_hex(post1); + for(i = 0;i<8;i++){ + hex_post1[i] = return_hex[i]; + } + array_to_hex(post2); + for(i = 0;i<8;i++){ + hex_post2[i] = return_hex[i]; + } + array_to_hex(get_same_position); + for(i = 0;i<8;i++){ + hex_post_cut[i] = return_hex[i]; + } + for(i = 0;i<8;i++){ + _temppic[i] = hex_post1[i] + hex_post2[i] + hex_post_cut[i]; + } + } + + } +} + + +// function Test! + int main(){ //-------------------------------------------------------------------Main Function. bt.baud(9600); float Vx; @@ -63,8 +690,12 @@ int SW,iVxVy,iSW,n = 0; int uli = 0; char m; + int rgb[3] = {10,0,0}; + int state_place_ship = 0; + dotmatrix.init(); while(1) { + dotmatrix.display_pic(_temppic,rgb); if(iShipType<8){ Vx = VRx.read() * 1024; Vy = VRy.read() * 1024; @@ -81,6 +712,18 @@ } if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch. Select_Position(m); + if(state_place_ship == 0){ + place_ship2_4(0,m); + } + else if(state_place_ship == 1){ + place_ship3(m); + } + else if(state_place_ship == 2){ + place_ship2_4(1,m); + } + else if(state_place_ship == 3 || state_place_ship == 4){ + place_ship2_4(2,m); + } printf("select_Position = %d\n",select_Position); check_ready(); iVxVy = 1; @@ -88,11 +731,34 @@ } if(SW==0 && iSW==0){ //______________________________________________If switch pressing by human. printf("Wait!\n"); + check_ready(); if(get_ship_platform(select_Position,ship_SelectType[iShipType])==0){ //__Please read explanation in function get_ship_platform. printf("Please input correct position\n"); }else{ iShipType++; + if(state_place_ship == 0){ + for(j=0;j<8;j++){ + pic[j] = temppic3[0][j]+pic[j]; + } + } + else if(state_place_ship == 1){ + for(j=0;j<8;j++){ + pic[j] = temppic2[j]+pic[j]; + } + } + else if(state_place_ship == 2 || state_place_ship == 3){ + for(j=0;j<8;j++){ + pic[j] = temppic2[j]+pic[j]; + } + } + + else if(state_place_ship == 4 || state_place_ship == 5 || state_place_ship == 6 || state_place_ship == 7){ + for(j=0;j<8;j++){ + pic[j] = temppic2[j]+pic[j]; + } + } + state_place_ship = state_place_ship + 1; } check_ready(); iSW = 1;