void mouse(int button, int state, int x, int y) { // We convert windows mouse coords to out openGL coords mouse_x = (18 * (float) ((float)x/(float)Win_x))/6; mouse_y = (18 * (float) ((float)y/(float)Win_y))/6; // What square have they clicked in? object_select = mouse_x + mouse_y * 3; if ( start_game == 0) { if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) { player = 1; computer = -1; init_game(); return; } if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { player = -1; computer = 1; init_game(); computer_move(); return; } } if ( start_game == 1) { if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { if (win == 0) { if (box_map[ object_select ] == 0) { box_map[ object_select ] = player; win = check_move(); if (win == 1) { start_game = 0; return; } computer_move(); win = check_move(); if (win == 1) { win = -1; start_game = 0; } } } } } if ( win == 2 )start_game = 0; }
int main() { /*------------------------------main function------------------------------*/ setcursortype(NOCURSOR); create_world(); int i; srand((unsigned)time(NULL)); for(;;) { ModTick(); check_wall();//checck_crash set_block(); new_block(); check_move();//move_block(); i++; Sleep(1); if (i>399) { i=0; move_block(DOWN); } gotoxy(0,0); printf("%3d",b_x); gotoxy(0,1); printf("%3d",b_y); gotoxy(0,3); printf("%3d",b_type); } gotoxy(0,0); getch(); } /*------------------------------main function------------------------------*/
void shape_hard_drop(){ while(true){ if(!check_move(s, b, 0, 1, 0)) break; s.y++; } }
void drop_loop(void * tp_void_ptr){ thread_params * tp_ptr = (thread_params *) tp_void_ptr; while(* tp_ptr->running > 0){ if(check_move(*tp_ptr->s, *tp_ptr->b, 0, 1, 0)){ tp_ptr->s->y++; }else{ if(add_to_board(*tp_ptr->s, tp_ptr->b) <= 0){ * tp_ptr->running = -1; break; } int clears = check_lines(tp_ptr->b); if(clears > 0){ mvprintw(40 + DEBUG_CNT++, 0, "Cleared lines %d", clears); } * tp_ptr->lines_cleared = * tp_ptr->lines_cleared + clears; new_shape(tp_ptr->s, tp_ptr->bucket); * tp_ptr->hold_cnt = 0; } //need method of changing this value as levels increase usleep(* tp_ptr->drop_speed); //100ms } }
void shape_rotate(){ if(check_move(s, b, 0, 0, 1)){ s.rotation++; if(s.rotation == 4){ s.rotation = 0; } } }
/** * generic acl checking for different methods (basic set of methods supported) * returns null if no err */ static dav_error *check_methods(request_rec *r, const dav_resource *resource, davacl_dir_cfg *conf) { const dav_hooks_repository *repos = REPOS(conf); if (repos == NULL || resource == NULL) return dav_acl_privilege_error(r, "unknown", NULL); switch (r->method_number) { default: if (r->method_number == iM_ACL) { return check_acl(r, resource, conf, repos); } else if (r->method_number == iM_HEAD) { return check_get(r, resource, conf, repos); } else { TRACE(r, "Unknown methdod:%d", r->method_number); return NULL; } break; case M_PUT: return check_put(r, resource, conf, repos); case M_PROPPATCH: return check_proppatch(r, resource, conf, repos); case M_MKCOL: return check_mkcol(r, resource, conf, repos); case M_PROPFIND: /* done with individual properties within dav_get_props() and * dav_get_allprops */ return NULL; case M_DELETE: return check_delete(r, resource, conf, repos); case M_OPTIONS: case M_GET: return check_get(r, resource, conf, repos); case M_COPY: return check_copy(r, resource, conf, repos); case M_MOVE: return check_move(r, resource, conf, repos); case M_LOCK: return check_lock(r, resource, conf, repos); case M_UNLOCK: return check_unlock(r, resource, conf, repos); } }
void Stepper_Control::y_callback() { float x_pos = float(this->top->get_pos()); float y_pos = float(this->bottom->get_pos()); if (!move->at_target(x_pos, y_pos)) { if (move->dir.y > 0.0) bottom->step(); PITimer2.period(move->calcdty()); } else { check_move(); } }
void Stepper_Control::x_callback() { float x_pos = float(this->top->get_pos()); float y_pos = float(this->bottom->get_pos()); if (!move->at_target(x_pos, y_pos)) { if (move->dir.x > 0.0) top->step(); PITimer1.period(move->calcdtx()); //Serial.println( (move->dt.x-move->t0)*1000 ); } else { check_move(); } }
static void new_block() { row = 0; col = 6; rot = 0; type = next_type; next_type = rand()%7; trigger_event("NewBlock", (void*)next_type); if(check_move()) trigger_event("GameOver", NULL); }
static int move(int dir) { switch(dir) { case LEFT: col--; break; case RIGHT: col++; break; case DOWN: row++; break; case ROTATE: rot = (rot+1)%4; break; } int rc = check_move(); if(rc == 1) { switch(dir) { case LEFT: col++; break; case RIGHT: col--; break; case DOWN: row--; break; case ROTATE: rot = rot == 0 ? 3 : rot-1; break; } if(dir == DOWN) { cement_block(); trigger_event("BlockHit", NULL); int lines = check_lines(); if(lines) calc_score(lines); new_block(); } } else { trigger_event("MoveSucceeded", (void*)dir); } return rc; }
//checks for next moves. If viable move found that doesn't lead to a duplicate board, new board shows this move and 1 is returned int find_next_move (hash *hash_array, Node *start, Node *latest_node, int parent_board [BOARD_HEIGHT][BOARD_WIDTH], int new_board[BOARD_HEIGHT][BOARD_WIDTH]){ for (int i = 0; i < BOARD_HEIGHT; ++i){ for (int j = 0; j < BOARD_WIDTH; ++j){ if (new_board[i][j] == 1){ if (check_move (up, i, j, hash_array, start, latest_node, parent_board, new_board)){ return 1; } if (check_move (right, i, j, hash_array, start, latest_node, parent_board, new_board)){ return 1; } if (check_move (left, i, j, hash_array, start, latest_node, parent_board, new_board)){ return 1; } if (check_move (down, i, j, hash_array, start, latest_node, parent_board, new_board)){ return 1; } } } } return 0; }
int main() { init(); while(true) { check_right(); check_left(); check_move(); check_scroll(); // wait(0.005); } }
int poirier_main(t_loop *data, int size) { if ((size = read(0, data->buff, data->ini.max)) < 0) quit_game(data); data->buff[size] = '\0'; if (data->pause == 0) data->timer = data->timer + 2 * (data->ini.game.level > 13 ? (13) : (data->ini.game.level)); if (size > 0) { if (check_move(data->buff, data, data->ptr)) init_new_form(data, 1); if (my_view(data)) quit_game(data); } return (0); }
int main(int argc, char* argv[]) { char *mapPath, *agentPath; //The location of the files int rows, cols, maxSteps; //Dimensions of the map and the max steps char** map; //Character map //Check the command line args if (argc != 4) { handler_exit(1); } mapPath = argv[1]; agentPath = argv[3]; long tempL; char** tempP = NULL; tempL = strtol(argv[2], tempP, 10); if (tempL < 1 || tempL > INT_MAX) { handler_exit(2); } //Set variables needed later in the program - max steps, map and agents maxSteps = (int)tempL; map = open_map_file(mapPath, &rows, &cols); get_num_agents(agentPath); agents = (AgentData *)malloc(sizeof(AgentData)*numAgents); get_agents(agentPath, rows, cols); //Set up signal handling signal(SIGINT, sig_handler); signal(SIGCHLD, sig_handler); //Start agents start_agents(rows, cols, map); //Start the game for (int i = 0; i < maxSteps; i++) { //Let each agent take turns, process their move and print the map for(int j = 0; j < numAgents; j++) { agent_move(j); check_move(map, j, rows, cols); print_map(map, rows, cols); } } //No agent succeeded with given steps handler_exit(10); }
bool move(int tile) { if (find_tile(tile)) { // If true switch it. if (check_move()) { int temp_r = tile_r; int temp_c = tile_c; int temp = board[tile_r][tile_c]; board[tile_r][tile_c] = board[blank_r][blank_c]; board[blank_r][blank_c] = temp; blank_r = temp_r; blank_c = temp_c; return true; } } return false; }
void Bomb::push_bomb() { float x = 0;//this->pos_x; float y = 0;//this->pos_y; int ret = EMPTY; if (this->pushed_dir == DIR_UP) y += -0.08f; else if (this->pushed_dir == DIR_BOTTOM) y += 0.08f; else if (this->pushed_dir == DIR_LEFT) x += -0.08f; else if (this->pushed_dir == DIR_RIGHT) x += 0.08f; ret = check_move(x * 3 + this->pos_x, y + this->pos_y); if (position_is_player(x + this->pos_x, y + this->pos_y) == true) { this->pos_x = this->pos_x; this->pos_y = this->pos_y; this->pushed = false; } else if (ret == EMPTY) { if ((int)this->pos_x != (int)(x + this->pos_x) || (int)this->pos_y != (int)(y + this->pos_y)) { delete main_event->map[(int)(this->pos_y + y)][(int)(this->pos_x + x)]; main_event->map[(int)(this->pos_y + y)][(int)(this->pos_x + x)] = NULL; main_event->map[(int)(this->pos_y + y)][(int)(this->pos_x + x)] = main_event->map[(int)this->pos_y][(int)this->pos_x]; main_event->map[(int)this->pos_y][(int)this->pos_x] = Factory::create_empty((int)this->pos_x, (int)this->pos_y); this->pos_x = x + this->pos_x; this->pos_y = y + this->pos_y; } else { this->pos_x = x + this->pos_x; this->pos_y = y + this->pos_y; } } else if (ret == FIRE) { // check if player this->timer = 0; this->pushed = false; } else this->pushed = false; }
int check_possibility(int player, int turn){ move m; for (m.y=1; m.y<=15; m.y++){ for (m.x=1; m.x<15; m.x++){ for (m.piece=0; m.piece<21; m.piece++){ for(m.rotate=0; m.rotate<8; m.rotate++){ if(check_move(player, turn, m) == 0){ if (show_hint){ printf("First-found possible move: %c%c%c%c\n", ((m.x<10) ? m.x+'0' : m.x+'a'-10), ((m.y<10) ? m.y+'0' : m.y+'a'-10), m.piece+'a', m.rotate+'0'); } return TRUE; } } } } } return FALSE; }
void Stepper_Control::cruise(int steps_top, int steps_bottom) { int ptop = this->top->get_pos(); int pbottom = this->bottom->get_pos(); Vec start; float v; if (moves.size() <= 0) { start = Vec(float(ptop),float(pbottom)); v = V_MIN; } else { Movement *first = moves.get_first(); start = first->stop; v = first->stopv; } Vec steps = Vec(float(steps_top), float(steps_bottom)); LinearMovement *cruise; cruise = new LinearMovement(start, 0.0, steps, v, v); moves.push(cruise); check_move(); }
void move_list(t_win *win, t_ios *term, int x, int y) { char *buf; int select; t_wsize ws; int resize; if (tcsetattr(0, TCSADRAIN, term) == -1) leave_term(); buf = ft_memalloc(5); win->stop = 0; read(0, buf, 3); resize = reset_val(term, &ws, 0); if (resize != 0) { y = win->position / win->max_y; x = win->position % win->max_y; check_move(buf, &y, &x, win); select = check_buf(buf, win, win->position, term); get_position(win, select, &x, &y); print_arg(&(win->arg), win, win->position, find_space(win->arg)); } free(buf); }
void g1_trike_class::think() { if (!alive()) { unoccupy_location(); request_remove(); return; } if (attack_target.valid()) { request_think(); dest_x = attack_target->x; dest_y = attack_target->y; stagger=0; path_cos = dest_x - x; //cheat and calculate path from current position path_sin = dest_y - y; path_tan_phi = attack_target->h - h; stagger = 0; path_pos = 0; path_len = (float)sqrt(path_cos*path_cos + path_sin*path_sin); i4_float dist_w = 1.0f/path_len; path_cos *= dist_w; path_sin *= dist_w; path_tan_phi *= dist_w; i4_float dist=0, dtheta=0, dx=0, dy=0,dz=0; speed*=1.3f; suggest_move(dist, dtheta, dx, dy, 0); g1_object_class * blocking=0; check_move(dx,dy,dz,blocking); move(dx,dy,dz); const i4_float KILL_RADIUS=0.5; //The trike is a self-destructing attacker //if we've run into the guy if (x > dest_x-KILL_RADIUS && y > dest_y-KILL_RADIUS && x < dest_x+KILL_RADIUS && y < dest_y+KILL_RADIUS) { int damage=g1_trike_def.get_damage_map()->get_damage_for(attack_target.get()->id); //hurt the dude attack_target->damage(this,damage,i4_3d_vector(dx,dy,0)); //throw some gibs g1_shrapnel_class * shrapnel = NULL; shrapnel = (g1_shrapnel_class *)g1_create_object(shrapnel_type); if (shrapnel) { i4_float rx,ry,rh; rx = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f; ry = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f; rh = (i4_rand()&0xFFFF)/((i4_float)0xFFFF) * 0.2f; shrapnel->setup(x+rx,y+ry,h + rh,6,i4_T); } //unoccupy_location(); //request_remove(); //health=0;//be shure that we aren't still alive for the next tick this->damage(this,health,i4_3d_vector(-dx,-dy,0)); return; } return; } else if (next_path.valid()) { //Need a new target because our's just blew away without our help... if (speed>(defaults->speed+defaults->accel)) { speed-=defaults->accel; } //slow down again. g1_path_object_class * next=(g1_path_object_class *)next_path.get(); if (dest_x!=next->x || dest_y != next->y) { return_to_path(); //Maybe called a bit too often, but shoudn't hurt. } } g1_map_piece_class::think(); }
double twenty_lines(){ clear(); int board_x = 15; int board_y = 1; int board_width = 10; int board_height = 20; //board boarders int i; for(i = 0; i < board_height; i++){ mvprintw(i + board_y, board_x, "|"); mvprintw(i + board_y, (board_width * 2) + (board_x) + 1, "|", i); } mvprintw(board_y + board_height, board_x, "+"); for(i = 1; i < (board_width * 2) + 1; i++) mvprintw(board_y + board_height, i + board_x, "-"); mvprintw(board_y + board_height, board_x + (board_width * 2) + 1, "+"); //bucket init_bucket(&bu); //shape new_shape(&s, &bu); //board b = init_board(board_width, board_height); int running = 1; int lines = 0; //drop loop params thread_params tp; tp.b = &b; tp.s = &s; tp.bucket = &bu; tp.hold_cnt = &hold_cnt; tp.drop_speed = &drop_speed; tp.running = &running; tp.lines_cleared = &lines; pthread_t pth; pthread_create(&pth, NULL, drop_loop, (void * ) &tp); struct timeval tval_before, tval_after, tval_result; gettimeofday(&tval_before, NULL); char tmbuf[64]; // game loop while(running > 0 && lines < 10){ timeout(100); draw_board(b, board_x, board_y); draw_hold(h_s, hold_cnt, 1, 1); draw_shape(s, board_x, board_y); draw_bucket(&bu, 45, 1); mvprintw(10, 2, "Lines: %d", lines); gettimeofday(&tval_after, NULL); timersub(&tval_after, &tval_before, &tval_result); strftime(tmbuf, sizeof tmbuf, "%M:%S", localtime(&tval_result.tv_sec)); mvprintw(14, 2, " "); mvprintw(14, 2, "%s.%02d", tmbuf, tval_result.tv_usec/10000); int ch = getch(); if(ch != ERR){ //hold - temporarily cycles trough if(ch == 'x'){ s.type++; if(s.type == 7) s.type = 0; //left }else if(ch == KEY_LEFT && check_move(s, b, -1, 0, 0)){ s.x--; //right }else if(ch == KEY_RIGHT && check_move(s, b, 1, 0, 0)){ s.x++; //rotate }else if(ch == KEY_UP && check_move(s, b, 0, 0, 1)){ s.rotation++; if(s.rotation == 4) s.rotation = 0; //hard drop }else if(ch == ' '){ while(true){ if(!check_move(s, b, 0, 1, 0)) break; s.y++; } //hold }else if(ch == 'z' && hold_cnt < 1){ hold(&s, &h_s, &bu); hold_cnt++; } } refresh(); if(DEBUG_CNT == 20) DEBUG_CNT = 0; } //stop drop loop running = -1; //delay for drop loop to stop usleep(1000000); clear(); return 1; }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen, game, bytes_recv,bytes_sent,line,col,count, flag,aux,aux2; struct sockaddr_in serv_addr, cli_addr; char *client_name; char buffer[SIZE_BUFFER]; char choice,symbol,client_symbol; symbol='X'; client_symbol='O'; game=0; client_name=(char*)malloc(NAME_SIZE*sizeof(char)); if (argc < 2) { printf("ERROR : INVALID USAGE\n"); printf("(%s port \n",argv[0]); return -1; } sockfd = socket(AF_INET,SOCK_STREAM,0); if (sockfd < 0){ perror("ERROR opening socket"); return -1; } bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){ perror("ERROR on binding\n"); return -1; } if(listen(sockfd,5)<0) { perror("ERROR : FAILED TO LISTEN\n"); return -1; } clilen = sizeof(cli_addr); system("clear"); server_header(); do { bzero(client_name,NAME_SIZE); printf("Waiting for a chalenger...\n"); newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen); if (newsockfd < 0){ perror("ERROR on accept"); return -1; } bytes_recv=read(newsockfd,client_name,NAME_SIZE); if(bytes_recv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } printf("New chalenger found : %s\n",client_name); printf("Do you want to play with this challenger (Y/N)? : "); scanf(" %c",&choice); bzero(buffer,SIZE_BUFFER); if(choice=='Y' || choice=='y') { strcpy(buffer,"01"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } bzero(buffer,SIZE_BUFFER); game=1; system("clear"); } else if(choice=='N' || choice=='n') { strcpy(buffer,"00"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } close(newsockfd); continue; } else { printf("Option not available\n"); return -1; } } while(game==0); init(); server_header(); display_board(); count=0; flag=0; aux=0; aux2=0; while (count<9) { if(flag==0) { do{ printf("Your move...\n"); printf("Line (1 to 3) : "); scanf("%d",&line); printf("Column (1 to 3) : "); scanf("%d",&col); }while(check_move(line,col)==0); move(line,col,symbol); system("clear"); server_header(); display_board(); buffer[0]=line+'0'; buffer[1]=col+'0'; bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } bzero(buffer,SIZE_BUFFER); count++; } else { printf("\nThe challenger’s move...\n"); bytes_recv=read(newsockfd,buffer,SIZE_BUFFER); if(bytes_recv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } line=buffer[0]-'0'; col=buffer[1]-'0'; move(line,col,client_symbol); system("clear"); server_header(); display_board(); count++; } if(count>4) { // min 5 moves to found a winner aux=check_winner(symbol); aux2=check_winner(client_symbol); if(aux==1) { printf("You won the game\n"); strcpy(buffer,"00"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } break; } else if(aux2==1) { printf("You lost the game\n"); strcpy(buffer,"01"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } break; } else { strcpy(buffer,"22"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } } } if(flag==1) flag=0; else flag=1; } if(aux==0 && aux2==0) { printf("The game ended with a draw\n"); strcpy(buffer,"11"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } } strcpy(buffer,"22"); bytes_sent=write(newsockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } close(sockfd); close(newsockfd); free(client_name); return 0; }
void Stepper_Control::linear_move(int steps_top, int steps_bottom) { Vec start; if (moves.size() <= 0) { int ptop = this->top->get_pos(); int pbottom = this->bottom->get_pos(); start = Vec(float(ptop),float(pbottom)); } else { Movement *first = moves.get_first(); start = first->stop; } Vec steps = Vec(float(steps_top), float(steps_bottom)); Vec stop = start+steps; float stopv = V_MAX; float startv = V_MIN; float acc_time = ACCTIME; float steps2acc = LinearMovement::calc_steps2acc(acc_time, startv, stopv); //Serial.print("steps2acc: "); //Serial.println(steps2acc); LinearMovement *acc; LinearMovement *cruise; LinearMovement *decc; Vec dir = stop - start; dir.normalise_max(); /*if (dir.x > dir.y) { dir.y = dir.y*(1/dir.x); dir.x = 1.0; } else if (dir.x > dir.y) { dir.x = dir.x*(1/dir.y); dir.y = 1.0; } else { dir.x = 1.0; dir.y = 1.0; }*/ Vec acc_steps = Vec(steps2acc*dir.x, steps2acc*dir.y); acc = new LinearMovement(start, acc_time, acc_steps, startv, stopv); //cruise = new LinearMovement(acc, 0.0, steps-acc_steps-acc_steps, stopv); cruise = new LinearMovement(start+acc_steps, 0.0, steps-acc_steps-acc_steps, stopv, stopv); //decc = new LinearMovement(cruise, acc_time, acc_steps, startv); decc = new LinearMovement(stop-acc_steps, acc_time, acc_steps, stopv, startv); moves.push(acc); moves.push(cruise); moves.push(decc); check_move(); /*Movement *tmp; for (int i = 0;i<moves.size();i++) { tmp = moves.get(i); Serial.print(i); Serial.print(" id: "); Serial.print(tmp->id); Serial.print(" x0: "); Serial.print(tmp->start.x); Serial.print(" x1: "); Serial.print(tmp->stop.x); Serial.print(" v0: "); Serial.print(tmp->startv); Serial.print(" v1: "); Serial.println(tmp->stopv); }*/ }
void shape_right(){ if(check_move(s, b, 1, 0, 0)) s.x++; }
void shape_left(){ if(check_move(s, b, -1, 0, 0)) s.x--; }
int do_game(){ int c, x, y, r, player, turn; char code[6], prev_code[6]; // ------------------------------------------------------------ // start! show_board(); player = first_player; turn = 0; prev_code[0] = 0; while(prompt(player, code, prev_code, !check_possibility(player, turn), turn)){ move m; int e, x_offset, y_offset; if(code[0] == 0) return TERMINATE_WRONG; // ctrl+d // retry if invalid code while(!check_code(code)){ if(interactive(player)){ prompt(player, code, prev_code, FALSE, turn); } else { printf("Invalid move on serial port.\n"); return player; } if(code[0] == 0) return TERMINATE_WRONG; } if(code[0] == 0) return TERMINATE_WRONG; // ctrl+d if(prev_code[0] == 0) strcpy(p1move1, code); if(strcmp(prev_code, "0000") == 0 && strcmp(code, "0000") == 0){ printf("Both pass!\n"); show_hint = FALSE; if(! (check_possibility(player, turn) || check_possibility(next_player(player), turn-1))){ return TERMINATE_NORMAL; } return TERMINATE_NORMAL; // is this OK? } strcpy(prev_code, code); // pass if(strcmp(code, "0000") == 0){ if(turn >= 2){ player = next_player(player); turn++; continue; } else { printf("First move must not be a pass.\n"); return player; } } m = decode_code(code); c = m.piece; r = m.rotate; x_offset = m.x-2; y_offset = m.y-2; if((e = check_move(player, turn, m)) != 0){ show_error(e); return player; } // OK, now place the move for(y=0; y<5; y++){ for(x=0; x<5; x++){ int b; b = pieces[c][0][rotate[r][y][x]]; if (b==1) board[y_offset+y][x_offset+x] = player; } } available[player-1][c] = 0; if(remaining_size(player)==0){ printf("Player %d won the game, since the player has no more pieces.\n", player); return TERMINATE_NORMAL; } // show the board & next player show_board(); player = next_player(player); turn++; } printf("Player %d timed out.\n", player); return player; }
int main (int argc,char *argv[] ) { int sockfd, port_nu,n,count,bytes_sent,bytes_rcv,line,col,flag,choice_symb; struct sockaddr_in serv_addr; // basic structures for all syscalls and functions that deal with internet addresses struct hostent *server; // represent an entry in the hosts database char *name; char symbol,serv_symbol; char server_ip[16]; char buffer[SIZE_BUFFER]; name=(char*)malloc(NAME_SIZE*sizeof(char)); if(argc<3) { printf("ERROR : INVALID USAGE \n(%s host port )\n",argv[0]); return -1; } port_nu=atoi(argv[2]); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { /* Create the socket */ printf("ERROR CREATING SOCKET\n"); return -1; } server = gethostbyname(argv[1]); if(server==NULL) { printf("ERROR : NO SUCH HOST\n"); return -1; } bzero((char*)&serv_addr,sizeof(serv_addr)); //Initializaton by write zero-value bites /* Give the socket a name. */ serv_addr.sin_family=AF_INET; bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length); serv_addr.sin_port = htons(port_nu); /* Client-Server comunication */ if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) { printf("ERROR connecting"); return -1; } system("clear"); client_header(); printf("Enter your Name : "); scanf("%s",name); n=write(sockfd,name,NAME_SIZE); if(n<0) { printf("ERROR writing to socket"); return -1; } inet_ntop(AF_INET,&serv_addr.sin_addr,server_ip,sizeof(serv_addr)); printf("Address of a defender : %s\n",server_ip); n=read(sockfd,buffer,SIZE_BUFFER); if(n<0) { printf("ERROR reading from socket"); return -1; } if(strcmp(buffer,"00")==0) { printf("That defender denied you’re challenge.\n"); return -1; } else if(strcmp(buffer,"01")==0) { printf("That defender accepted you’re challenge.\n"); sleep(2); system("clear"); } client_header(); printf("Doing a sortition\n"); bytes_rcv=read(sockfd,buffer,SIZE_BUFFER); if(bytes_rcv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } if(strcmp(buffer,"00")==0) { printf("Defender plays first...\n"); bytes_rcv=read(sockfd,buffer,SIZE_BUFFER); if(bytes_rcv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } if(strcmp(buffer,"01")==0) { symbol='O'; serv_symbol='X'; } else { symbol='X'; serv_symbol='O'; } flag=1; printf("You play with %c.\n",symbol); sleep(2); }else { printf("You play first\n"); do{ printf("Choice symbol (1-X | 2-O) : "); scanf("%d",&choice_symb); if(choice_symb!=1 & choice_symb!=2) printf("Ilegal option, please choice again.\n"); }while(choice_symb!=1 & choice_symb!=2); if(choice_symb==1) { symbol='X'; serv_symbol='O'; strcpy(buffer,"01"); bytes_sent=write(sockfd,buffer,SIZE_BUFFER); } else { symbol='O'; serv_symbol='X'; strcpy(buffer,"02"); bytes_sent=write(sockfd,buffer,SIZE_BUFFER); } if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } flag=0; } count=0; system("clear"); init(); client_header(); display_board(); while(count<9) { if(flag==1) { bzero(buffer,SIZE_BUFFER); printf("The defender’s move...\n"); bytes_rcv=read(sockfd,buffer,SIZE_BUFFER); if(bytes_rcv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } line=buffer[0]-'0'; col=buffer[1]-'0'; move(line,col,serv_symbol); system("clear"); client_header(); display_board(); count++; } else { do { printf("Your move...\n"); printf("Line (1 to 3) : "); scanf("%d",&line); printf("Column (1 to 3) : "); scanf("%d",&col); }while(check_move(line,col)==0); system("clear"); move(line,col,symbol); client_header(); display_board(); bzero(buffer,SIZE_BUFFER); buffer[0]=line+'0'; buffer[1]=col+'0'; bytes_sent=write(sockfd,buffer,SIZE_BUFFER); if(bytes_sent<0) { perror("ERROR : FAILED TO SEND DATA INFORMATION"); return -1; } bzero(buffer,SIZE_BUFFER); count++; } if(count>4) { bytes_rcv=read(sockfd,buffer,SIZE_BUFFER); if(bytes_rcv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } if(strcmp(buffer,"00")==0) { printf("You lost the game\n"); break; } else if(strcmp(buffer,"01")==0) { printf("You won the game\n"); break; } } if(flag==1) flag=0; else flag=1; } bytes_rcv=read(sockfd,buffer,SIZE_BUFFER); if(bytes_rcv<0) { perror("ERROR : FAILED TO ACQUIRE DATA INFORMATION"); return -1; } if(strcmp(buffer,"11")==0) printf("TThe game ended with a draw\n"); close(sockfd); free(name); return 0; }