Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of project_ShipDot by
Diff: main.cpp
- Revision:
- 23:66a401850f2e
- Parent:
- 22:5bc894988f11
- Child:
- 24:03e12968119b
--- a/main.cpp Sun Dec 04 05:02:03 2016 +0000
+++ b/main.cpp Sun Dec 04 09:07:54 2016 +0000
@@ -3,7 +3,10 @@
/******************************************************************************/
#include "mbed.h"
#include "ColorMbed.h"
-
+
+// Setting
+int ifUrJirat = 1;
+
// Initail I/O.
AnalogIn VRx(A0);
AnalogIn VRy(A1);
@@ -14,28 +17,36 @@
// Initial function.
char posit2char(int posit);
char check_joys(int jx,int jy);
+int IamWin();
+int TellUrEnemy_uWin();
int char2posit(char cha);
-int IamWin();
-int protect_ship(int head_position,int ship_type);
-int TellUrEnemy_uWin();
int check_joys_int(int jx,int jy);
int check_correctPosition(int X,int Y);
+int protect_ship(int head_position,int ship_type);
int get_ship_platform(int input_position,int ship_type);
void atk_enemy();
void enemy_atk();
+void check_end();
void check_ready();
void mark_ship_destroyed();
-void Select_Position(char M);
+void Select_Position(char M,int SetOrPlay);
void get_atk_platform(int atkpos);
-void check_end();
+
//inessential
void print_ship_destroyed();
void print_enemy_platform();
void print_ship_platform();
void print_atk_platform();
-// Setting
-int ifUrJirat = 0;
+// Nasarun's function.
+int _power(int number , int power);
+void hex_to_array(int* ar);
+void place_ship3(char dir);
+void array_to_hex(int ar1[8][8]);
+void place_ship2_4(int ship,char dir);
+void ch_same_position(int ar1[8][8],int ar2[8][8]);
+void discard_same_position(int ar1[8][8],int ar2[8][8]);
+
// Initial variable.
int check_ship_underATK = 0;
int select_Position = 0;
@@ -57,7 +68,6 @@
int ship3[3] = {0};
int ship2[2][2] = {{0}};
int ship1[4] = {0};
-
//fuction nasrun
int return_position[8][8] ;
int for_hex = 0;
@@ -70,619 +80,18 @@
{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 temppic3[7][8] = {{0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00},{0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},{0x01,0x00,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;
@@ -690,7 +99,7 @@
int SW,iVxVy,iSW,n = 0;
int uli = 0;
char m;
- int rgb[3] = {10,0,0};
+ int rgb[3] = {60,0,0};
int state_place_ship = 0;
dotmatrix.init();
@@ -711,18 +120,30 @@
check_ready();
}
if(m!=NULL && iVxVy==0){ //__________________________________________ Get position that wait for press switch.
- Select_Position(m);
+ Select_Position(m,0);
if(state_place_ship == 0){
- place_ship2_4(0,m);
+ place_ship2_4(0,m); // 4
}
else if(state_place_ship == 1){
- place_ship3(m);
+ place_ship3(m); // 3
}
else if(state_place_ship == 2){
- place_ship2_4(1,m);
+ place_ship2_4(1,m); // 2
+ }
+ else if(state_place_ship == 3){
+ place_ship2_4(2,m); //2
+ }
+ else if(state_place_ship == 4){
+ place_ship2_4(3,m); // 1
}
- else if(state_place_ship == 3 || state_place_ship == 4){
- place_ship2_4(2,m);
+ else if(state_place_ship == 5){
+ place_ship2_4(4,m); // 1
+ }
+ else if(state_place_ship == 6){
+ place_ship2_4(5,m); // 1
+ }
+ else if(state_place_ship == 7){
+ place_ship2_4(6,m); // 1
}
printf("select_Position = %d\n",select_Position);
check_ready();
@@ -739,26 +160,54 @@
iShipType++;
if(state_place_ship == 0){
for(j=0;j<8;j++){
+ printf("state_place_ship == 0 ++\n");
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++){
+ printf("state_place_ship == 1 ++\n");
pic[j] = temppic2[j]+pic[j];
}
}
-
- else if(state_place_ship == 4 || state_place_ship == 5 || state_place_ship == 6 || state_place_ship == 7){
+ else if(state_place_ship == 2){
+ for(j=0;j<8;j++){
+ printf("state_place_ship == 2 ++\n");
+ pic[j] = temppic3[1][j]+pic[j];
+ }
+ }
+ else if(state_place_ship == 3){
+ for(j=0;j<8;j++){
+ printf("state_place_ship == 3 ++\n");
+ pic[j] = temppic3[2][j]+pic[j];
+ }
+ }
+ else if(state_place_ship == 4){
for(j=0;j<8;j++){
- pic[j] = temppic2[j]+pic[j];
+ printf("state_place_ship == 4 ++\n");
+ pic[j] = temppic3[3][j]+pic[j];
+ }
+ }
+ else if(state_place_ship == 5){
+ for(j=0;j<8;j++){
+ printf("state_place_ship == 5 ++\n");
+ pic[j] = temppic3[4][j]+pic[j];
+ }
+ }
+ else if(state_place_ship == 6){
+ for(j=0;j<8;j++){
+ printf("state_place_ship == 6 ++\n");
+ pic[j] = temppic3[5][j]+pic[j];
+ }
+ }
+ else if(state_place_ship == 7){
+ for(j=0;j<8;j++){
+ printf("state_place_ship == 7 ++\n");
+ pic[j] = temppic3[6][j]+pic[j];
}
}
state_place_ship = state_place_ship + 1;
+ select_Position = 0;
}
check_ready();
iSW = 1;
@@ -849,7 +298,7 @@
return 0;
}
}
-void Select_Position(char M){ //-------------------------------------------------Change position of cursor (Joystick).
+void Select_Position(char M,int SetOrPlay){ //-------------------------------------------------Change position of cursor (Joystick).
int protect_ships;
protect_ships = protect_ship(select_Position,ship_SelectType[iShipType]);
switch(M){
@@ -862,9 +311,10 @@
if(select_Position%10!=7){
if(protect_ships==1 or protect_ships==0){
select_Position = select_Position + 1;
- }//else{
- //printf("Not allow position.\n");
- //}
+ }
+ if(SetOrPlay==1){
+ select_Position = select_Position + 1;
+ }
}
break;
case 'w':
@@ -876,9 +326,10 @@
if(select_Position/10!=7){
if(protect_ships==1 or protect_ships==2){
select_Position = select_Position + 10;
- }//else{
- //printf("Not allow position.\n");
- //}
+ }
+ if(SetOrPlay==1){
+ select_Position = select_Position + 10;
+ }
}
break;
}
@@ -1074,7 +525,7 @@
}
}
if(m!=NULL && iVxVy==0){
- Select_Position(m);
+ Select_Position(m,1);
printf("select_Position = %d\n",select_Position);
bt.putc(posit2char(select_Position));
iVxVy = 1;
@@ -1131,4 +582,601 @@
chare = bt.getc();
if(chare=='q'){return 1;}else{return 0;}
}else{return 0;}
+}
+
+// ______________________________________________________________________________Nasarun's function.
+
+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 == 'd'){
+ printf("d\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 == 'a'){
+ printf("a\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 == 'd' && temppic3[ship][3] != 0x80 && temppic3[ship][4] != 0x80 ){
+ printf("d\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 == 'a' && temppic3[ship][3] != 0x01 && temppic3[ship][4] != 0x01 ){
+ printf("a\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];
+ }
+ }
+
+ }
}
\ No newline at end of file
