// Go home and retrieve disk then move to center void load_disk(void) { if (digitalRead(ACTUATOR_LIMIT) == HIGH) // Check if a disk is already on the actuator { delay(50); // debounce if (digitalRead(ACTUATOR_LIMIT) == HIGH) { get_disk(); // No disk detected so we load one } } findCenter(); delay(500); place_disk(); }
void loop() { while (Serial.available()) // If serial data is available on the port { inByte = (char)Serial.read(); // Read a byte if (echo) Serial.print(inByte); // If the scho is on then print it back to the host if (inByte == '\r') // Once a /r is recvd check to see if it's a good command { if (echo) Serial.println(); // Dummy empty line if (inData == "up") lift_arm(); else if (inData == "downs") down_seek(50, 0); // Lower arm until it finds bottom else if (inData == "down") down(50, ARM_DN_POSITION); // Blindly lower arm else if (inData == "pump on") pump(ON); // Start pump else if (inData == "pump off") pump(OFF); // Stop pump else if (inData == "vent") pump_release(); // Stop pump and vent the vacuum else if (inData == "left") baseLeft(16); // Move base to the left else if (inData == "center") findCenter(); // Move base to the right else if (inData == "right") baseRight(BASE_CENTER_POSITION);// Move base to the right else if (inData == "stop") baseStop(); // Stop base else if (inData == "home") left_home(); // Mode base left until it finds home else if (inData == "status") stats(); // Show current status else if (inData == "stats") stats(); // Show current stats else if (inData == "get disk") get_disk(); // Macro to get disk and lift to top else if (inData == "load disk") load_disk(); // Macro to get disk and move to center else if (inData == "unload disk") unload_disk(); // Macro to get disk and move to center else if (inData == "place disk") place_disk(); // Macro to lower disk into tray else if (inData == "debug on") debug_mode = true; // turn on debug mode, prints encoder values else if (inData == "debug off") debug_mode = false; // Turn off debug mode else if (inData == "echo on") echo = true; // Turn on echo mode else if (inData == "echo off") echo = false; // Turn off echo mode else // The string wasn't recognized so it was a bad command { Serial.print("Error, Unknown Command: "); // Show the error Serial.println(inData); // Echo the command } inData = ""; // Clear the buffer (string) if (echo) prompt(); // reprint the prompt } else inData += inByte; // If it's not a /r then keep adding to the string (buffer) } }
// min int min_node(int depth, int side, XY *move) { int best = INFINITY, value; int nmoves; XY moves[MOVENUM], opp[MOVENUM]; int pre_board[8][8]; int i; if (depth == DEPTH) return eval_func(); // 手を生成 nmoves = generate_moves(side, moves); // 手がないとき if (nmoves == 0) { if (generate_moves(-side, opp) == 0) // 相手もおけないときは終了 return get_terminal_value(); else // 違うときはパス moves[nmoves++] = PASSMOVE; } // たどっていく for (i = 0; i < nmoves; i++) { memcpy(pre_board, board, sizeof(board)); // 盤面の保存 place_disk(side, moves[i]); // 一手進める // 再帰(recursive) value = max_node(depth + 1, -side, move); memcpy(board, pre_board, sizeof(board)); // 戻す // 値の更新 if (value <= best) { best = value; if (depth == 0) *move = moves[i]; } } return best; }
int main(int argc, char **argv) { const int human_side = (argc >= 2) ? atoi(argv[1]) : 0; // 先攻と後攻の選択 XY moves[MOVENUM]; init_board(); // 盤面の初期化 print_board(); // 盤面の表示 srand((unsigned)time(NULL)); // 乱数列の初期化 int turn; for (turn = 1;; turn *= -1) // ターンを交代 { XY nextmove; // どちらも置けなくなったときは終了 if (generate_moves(turn, moves) == 0 && generate_moves(-turn, moves) == 0) break; if (turn == human_side) man_player(turn, &nextmove); else com_player(turn, &nextmove); // パスかどうかの判定 if (nextmove.x != PASSMOVE.x && nextmove.y != PASSMOVE.y) { printf("%s -> %c%c\n\n", (turn == BLACK ? "BLACK" : "WHITE"), 'a' + nextmove.x, '1' + nextmove.y); place_disk(turn, nextmove); // ディスクの設置 print_board(); // 盤面の表示 } } show_result(); // 結果の表示 return 0; }
int main(int argc, char **argv) { srand((unsigned) time(NULL)); int board[8][8];//グローバル変数ではなく、いちいち渡すことに const int random_side = (argc >= 2) ? atoi(argv[1]) : 0;//MANの先攻後攻の決定 init_board(board); int turn; int pass=0; for (turn = 1;; turn *= -1) {//先手の時はturn=1,後手の時はturn=-1 print_board(board); Pair legal_moves[60];//合法手を全て収納するための配列 const int nmoves = generate_all_legal_moves(turn, legal_moves,board);//空きマス0能時にnamoves=-1を返す ←一回目のここで既に何かミスってる if (nmoves == -1) break; // no empty square→ゲーム終了 if (nmoves == 0) { pass++; printf("turn = %d, move = Pass\n", turn);//パスの処理 if(pass>=2){ break; } continue; }else{//passの初期化 pass=0; } Pair move; if (turn == random_side) {//人間側の操作のとき。com同士ならこれはおこらない move = legal_moves[rand()%nmoves]; //ランダム選択 assert(is_legal_move(turn,move,board)); } else {//COM側の操作 move = goodmove(turn,board);//turnは少なくとも必要 assert(is_legal_move(turn,move,board));//念のため。最適解がちゃんとlegalmoveか確認 } place_disk(turn, move,board);//moveをそもそも受け取れていなかった printf("turn = %d, move = %c%c\n", turn, 'a' + move.x, '1' + move.y); }//breakするまでfor文は繰り返す judge(random,board);//先攻か後攻かを返す return 0; }