/* * the method checBoard looks for an specific word in the board, letter by letter, recursively * */ bool Boggle::checkBoard( const std::string _currentWord, char _board[MAX_BOARD_SIZE + 1][MAX_BOARD_SIZE + 2], bool _visited[MAX_BOARD_SIZE][MAX_BOARD_SIZE], int curIndex, int r, int c ) const { if (curIndex == MAX_WORD_SIZE) return false; if (_currentWord[curIndex] != _board[r][c]) return false; // this is not the letter you are looking for... if (_currentWord.length() -1 == curIndex) { return true; // we are done here, word found! } _visited[r][c] = true; // we mark it as visited in the board, so we dont repeat the same position for (int row=-1; row <= 1 ; row++){ for (int col=-1; col <= 1; col++){ if (row>=0 && col>=0) { // only valid positions on the board if (( row != r) || (col != c) ) { // which are not the one i am standing now if ( !_visited[row][col]) { // whic has not been visited if ('\0' != _board[row][col]){ // and its not a null (only valid positions) if (checkBoard(_currentWord,_board, _visited, curIndex + 1, row, col)) { return true; } } } } } } } return false; }
void TicTacGameBoard::buttonClicked() { if ( st != HumansTurn ) // not ready return; int i = buttons->findRef( (TicTacButton*)sender() ); TicTacButton *b = buttons->at(i); // get piece that was pressed if ( b->type() == TicTacButton::Blank ) { // empty piece? btArray->at(i) = TicTacButton::Circle; updateButtons(); if ( checkBoard( btArray ) == 0 ) // not a winning move? computerMove(); int s = checkBoard( btArray ); if ( s ) { // any winners yet? st = s == TicTacButton::Circle ? HumanWon : ComputerWon; emit finished(); } } }
runState_t Hturn(state_t st, pKind_t player) { //人のターン if((player==NONE)||(player > BK)) myError("undefined player\n"); st->player = player; checkBoard(st); if(st->activePiece == 0) return finish; point_t p = allocPoint(); runState_t rs = cont; movePieace(st, p); return rs; }
void TicTacGameBoard::computerMove() { int numButtons = nBoard*nBoard; int *altv = new int[numButtons]; // buttons alternatives int altc = 0; int stopHuman = -1; TicTacArray a = btArray->copy(); int i; for ( i=0; i<numButtons; i++ ) { // try all positions if ( a[i] != TicTacButton::Blank ) // already a piece there continue; a[i] = TicTacButton::Cross; // test if computer wins if ( checkBoard(&a) == a[i] ) { // computer will win st = ComputerWon; stopHuman = -1; break; } a[i] = TicTacButton::Circle; // test if human wins if ( checkBoard(&a) == a[i] ) { // oops... stopHuman = i; // remember position a[i] = TicTacButton::Blank; // restore button continue; // computer still might win } a[i] = TicTacButton::Blank; // restore button altv[altc++] = i; // remember alternative } if ( stopHuman >= 0 ) // must stop human from winning a[stopHuman] = TicTacButton::Cross; else if ( i == numButtons ) { // tried all alternatives if ( altc > 0 ) // set random piece a[altv[rand()%(altc--)]] = TicTacButton::Cross; if ( altc == 0 ) { // no more blanks st = NobodyWon; emit finished(); } } *btArray = a; // update model updateButtons(); // update buttons delete[] altv; }
//******************************************************************************************************** bool PlayerUser::setShips() { //static HandlerCmd hnd(vis); int stat = 0; while (stat != -1) { //getMyBoard()->drawRightBoard(vis); //getOpBoard()->drawLeftBoard(vis); //stat = hnd.handlerCmdPrep(this); if (stat == 1) { if (checkBoard()) return true; //else vis->PrintMsg("Board is'n valid!!!"); } } return false; }
int main() { int T,t,i,j,k,l; char board[4][5]; char tmp[5]; scanf("%d", &T); for(t=0;t<T;t++) { for(i=0;i<4;i++) scanf("%s", board[i]); //for(i=0;i<4;i++){ for(j=0;j<4;j++) printf("%c",board[i][j]); printf("\n"); } printf("Case #%d: ", t+1); if(checkBoard(board, 'X')) printf("X won"); else if(checkBoard(board, 'O')) printf("O won"); else if(checkComplete(board, '.')) printf("Draw"); else printf("Game has not completed"); printf("\n"); } return 0; }
/* * At the moment, the main() function is primarily used for testing and * debugging. */ int main() { clearArray(moving); clearArray(fixed); srand(0); /* Initialize random seed */ /* Debugging info */ int j; for (j = 0; j < 10; j++) { fixed[10][j] = 1; } printf("Initial board:\n"); printArray(&fixed, 24, 10); printf("Checking array\n"); checkBoard(); printArray(&fixed, 24, 10); printf("Points: %d \n", points); /* More debugging info */ printf("Printing moving board:\n"); printArray(&moving, 24, 10); printf("Adding random tile:\n"); int k; for (k = 0; k < 7; k++) { setRandomTile(); printArray(&moving, 24, 10); printf("\n\n\n"); clearArray(moving); } /* trying to move left */ setRandomTile(); printArray(&moving,24,10); printf("\n\n\n"); moveLeft(); printArray(&moving,24,10); /*move right*/ printf("\n\n\n"); moveRight(); printArray(&moving,24,10); getchar(); return 0; }
void multiPlayer() { do { system("CLS"); gameName(); draw(); setMove(getMove()); checkWin(); checkBoard(); }while(gameFinished!=true); printBoard(); system("PAUSE"); resetBoard(); }
void singlePlayer() { do { system("CLS"); gameName(); draw(); if(player==1) {setRmove(getRmove());} else {setMove(getMove());} checkWin(); checkBoard(); }while(gameFinished!=true); printBoard(); system("PAUSE"); resetBoard(); }
U64 Perft(int depth, S_BOARD *pos) { ASSERT(checkBoard(pos)); S_MOVELIST list[1]; GenerateAllMoves(pos, list); if(depth == 0) { return 1; } U64 nodes = ZERO64; int i; for (i = 0; i < list->count; i++) { if (MakeMove(pos, list->moves[i].move)) { nodes += Perft(depth - 1, pos); TakeMove(pos); } } return nodes; }
void TicTacGameBoard::theirMove(int space) { if (st != ComputersTurn) return; qDebug() << (qPrintable(QString("they moved to %1 of %2").arg(space).arg(nBoard*nBoard))); if (space>=nBoard*nBoard) return; qDebug() << (qPrintable(QString("Space %1 was %2").arg(space).arg((int)(btArray->at(space))))); (*btArray)[space] = TicTacButton::Cross; qDebug() << (qPrintable(QString("Set %1 to %2 in btArray").arg(space).arg((int)(btArray->at(space))))); updateButtons(); qDebug("buttons updated"); int s = checkBoard( btArray ); st = HumansTurn; qDebug("board checked"); if ( s ) { // any winners yet? st = s == TicTacButton::Circle ? HumanWon : ComputerWon; emit finished(); } qDebug("thoir move finished"); }
int main(int argc, char ** argv) { if(argc < 2 || ((argc == 2) && (strcmp(argv[1], "-h") == 0) )) { printf("LCD-related Commands:\n"); printf("\t-c (clear screen)\n"); printf("\t-t line1 line2\n"); printf("\t-bloff (backlight on)\n"); printf("\t-blon (backlight off)\n"); printf("\t-blfl (backlight flash)\n"); printf("\nSafety commands:\n"); printf("\t-safe (disable thrusters)\n"); printf("\t-unsafe (enable thrusters)\n"); printf("\t-safe [n] (safe thruster n)\n"); printf("\t-unsafe [n] (unsafe thruster n)\n"); printf("\t-diagon (runtime diagnostics on)\n"); printf("\t-diagoff (runtime diagnostics off)\n"); printf("\t-tstop (send zero speed command)\n"); printf("\nPower commands:\n"); printf("\t-hardkill (kill power)\n"); printf("\t-extpower (switch to external power source)\n"); printf("\t-intpower (switch to batteries)\n"); printf("\t-batton n (enable battery n)\n"); printf("\t-battoff n (disable battery n)\n"); printf("\t-setovr a b (configure overcurrent parameters)\n"); printf("\t-readovr (read overcurrent parameters)\n"); printf("\t-setspeed a b c d e f (set speeds)\n"); printf("\nSonar commands:\n"); printf("\t-bfstart (starts the Blackfin processor)\n"); printf("\t-bfstop (stops the Blackfin processor)\n"); printf("\t-bfreset (reboots the Blackfin processor)\n"); printf("\t-sonar (retrieve latest sonar telemetry)\n"); printf("\nOther commands:\n"); printf("\t-check (crude system check)\n"); printf("\t-status (show sensor readings)\n"); printf("\t-baron (enable bar outputs)\n"); printf("\t-baroff (disable bar outputs)\n"); printf("\t-baron [n] (enable bar output n)\n"); printf("\t-baroff [n] (disable bar output n)\n"); printf("\t-marker {1|2} (drop marker 1 or 2)\n"); printf("\t-s (begin start sequence)\n"); printf("\t-setbars n (set bar outputs)\n"); printf("\t-noblink (stop animation)\n"); printf("\t-redgreen (start red/green animation)\n"); printf("\t-redblue (start red/blue animation)\n"); printf("\nDVL commands:\n"); printf("\t-dvlon (enable dvl)\n"); printf("\t-dvloff (disable dvl)\n"); printf("\nPneumatics Commands:\n"); printf("\t-firetorp # (fire torpedo number #)\n"); printf("\t-armtorp # (arm torpedo number #)\n"); printf("\t-voidtorp # (void torpedo number #)\n"); printf("\t-extgrabber (close the grabber)\n"); printf("\t-retgrabber (open the grabber)\n"); printf("\t-voidgrabber (void the grabber)\n"); printf("\t-voidall (void all pneumatics)\n"); printf("\t-pneuoff (turn off pneumatics)\n"); printf("\nDerpy Commands:\n"); printf("\t-derpypwr {on|off} (turn on or off derpy power)\n"); printf("\t-setderpy # (set derpy speed)\n"); printf("\t-stopderpy (set derpy speed to not move)\n"); return -1; } int fd = openSensorBoard("/dev/ttyUSB0"); if(fd == -1) { printf("\nCould not find device!\n"); printf("Is the right device set?\n"); printf("Has the serial number of this FTDI chip been added to udev?\n"); printf("Because that needs to be done for every board we make.\n"); close(fd); return -1; } if(syncBoard(fd) != 0) { printf("\nCould not sync with board!\n"); close(fd); } if(strcmp(argv[1], "-readovr") == 0) { int a, b; if(readOvrParams(fd, &a, &b) != SB_OK) printf("Error reading parameters\n"); else printf("a=%d, b=%d\n", a, b); close(fd); return 0; } else if(strcmp(argv[1], "-setovr") == 0) { if(argc != 4) { printf("Bad number of arguments\n"); close(fd); exit(1); } printf("reply was 0x%02x\n", setOvrParams(fd, atoi(argv[2]), atoi(argv[3]))); } else if(strcmp(argv[1], "-bfreset") == 0) { printf("reply was 0x%02x\n", resetBlackfin(fd)); } else if(strcmp(argv[1], "-bfstart") == 0) { printf("reply was 0x%02x\n", startBlackfin(fd)); } else if(strcmp(argv[1], "-bfstop") == 0) { printf("reply was 0x%02x\n", stopBlackfin(fd)); } else if(strcmp(argv[1], "-sonar") == 0) { struct sonarData sd; getSonarData(fd, &sd); printf("Vector: \t<%5.4f %5.4f %5.4f>\n", sd.vectorX, sd.vectorY, sd.vectorZ); printf("Status: \t0x%02x\n", sd.status); printf("Range: \t%u\n", sd.range); printf("Timestamp:\t%u\n", sd.timeStampSec); printf("Sample No:\t%u\n", sd.timeStampUSec); // printf("reply was 0x%02x\n", resetBlackfin(fd)); } else if(strcmp(argv[1], "-magpwron") == 0) { int ret; if((ret = setMagPower(fd, 1)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-magpwroff") == 0) { int ret; if((ret = setMagPower(fd, 0)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-setspeed") == 0) { if(argc != 8) { printf("Bad number of arguments\n"); close(fd); exit(1); } printf("Ctrl-C to quit.\n"); while(1) { setSpeeds(fd, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), atoi(argv[7])); usleep(50000); } } else if(strcmp(argv[1], "-status") == 0) { int ret; int bStat = -1; int bEnable = -1; unsigned char temp[NUM_TEMP_SENSORS]; ret = readDepth(fd); if(ret == SB_ERROR) printf("Error reading depth!\n"); else printf("Depth : %d (%s)\n", ret, (ret > 50) ? "Seems OK" : "Check Sensor"); ret = readTemp(fd, temp); if(ret == SB_ERROR) printf("Error reading temperature!\n"); else { int i; printf("Temperature (C): "); for(i=0; i<NUM_TEMP_SENSORS; i++) if(temp[i] == 255) printf("%c%c ", '?','?'); else printf("%d ", temp[i]); printf("\n"); } ret = readThrusterState(fd); if(ret == SB_ERROR) printf("Error reading thruster state!\n"); else { printf("\nThruster state: 0x%02X\t", ret); printf("[%c%c%c%c%c%c]", (ret & THRUSTER1_ENABLED) ? '1' : '-', (ret & THRUSTER2_ENABLED) ? '2' : '-', (ret & THRUSTER3_ENABLED) ? '3' : '-', (ret & THRUSTER4_ENABLED) ? '4' : '-', (ret & THRUSTER5_ENABLED) ? '5' : '-', (ret & THRUSTER6_ENABLED) ? '6' : '-'); if(ret == 0x00) printf(" (All safe)"); if(ret == 0x3F) printf(" (All unsafe)"); } ret = readOvrState(fd); if(ret == SB_ERROR) printf("\nError reading overcurrent register!\n"); else { printf("\nOver-current: \t0x%02X", ret); printf("\t[%c%c%c%c%c%c]", (ret & THRUSTER1_OVR) ? '1' : '-', (ret & THRUSTER2_OVR) ? '2' : '-', (ret & THRUSTER3_OVR) ? '3' : '-', (ret & THRUSTER4_OVR) ? '4' : '-', (ret & THRUSTER5_OVR) ? '5' : '-', (ret & THRUSTER6_OVR) ? '6' : '-'); if(ret == 0x00) printf(" (All ok)"); if(ret == 0xFF) printf(" (All busted)"); } ret = readBarState(fd); if(ret == SB_ERROR) printf("Error reading bar state!\n"); else { printf("\nBar state :\t0x%02X", ret); printf("\t[%c%c%c%c%c%c%c%c]", (ret & BAR1_ENABLED) ? '1' : '-', (ret & BAR2_ENABLED) ? '2' : '-', (ret & BAR3_ENABLED) ? '3' : '-', (ret & BAR4_ENABLED) ? '4' : '-', (ret & BAR5_ENABLED) ? '5' : '-', (ret & BAR6_ENABLED) ? '6' : '-', (ret & BAR7_ENABLED) ? '7' : '-', (ret & BAR8_ENABLED) ? '8' : '-'); if(ret == 0x00) printf(" (All off)"); if(ret == 0xFF) printf(" (All on)"); } ret = readBatteryEnables(fd); if(ret == SB_ERROR) printf("Error reading battery enables state!\n"); else { printf("\nBatt enables: 0x%02X\t", ret); printf("[%c%c%c%c%c%c]", (ret & BATT1_ENABLED) ? '1' : '-', (ret & BATT2_ENABLED) ? '2' : '-', (ret & BATT3_ENABLED) ? '3' : '-', (ret & BATT4_ENABLED) ? '4' : '-', (ret & BATT5_ENABLED) ? '5' : '-', (ret & BATT6_ENABLED) ? '6' : '-'); if(ret == 0x00) printf(" (All off ? ? ? ? ?)"); if(ret == 0x1F) printf(" (All on)"); bEnable = ret; } ret = readStatus(fd); if(ret == SB_ERROR) printf("Error reading board status!\n"); else { printf("\nBatt in use : 0x%02X\t", ret); printf("[%c%c%c%c%c%c]", (ret & BATT1_INUSE) ? '1' : '-', (ret & BATT2_INUSE) ? '2' : '-', (ret & BATT3_INUSE) ? '3' : '-', (ret & BATT4_INUSE) ? '4' : '-', (ret & BATT5_INUSE) ? '5' : '-', (ret & BATT6_INUSE) ? '6' : '-'); bStat = ret; printf("\n\nStatus: 0x%02X\n", ret); printf("\t%s\n\t%s\n\t%s\n", (ret & STATUS_WATER) ? "Water present" : "No water detected.", (ret & STATUS_STARTSW) ? "Start switch on" : "Start switch off", (ret & STATUS_KILLSW) ? "Kill magnet present" : "No kill magnet"); } struct powerInfo info; ret = readMotorCurrents(fd, &info); if(ret == SB_OK) { int i; printf("\nOutput currents:\n\t[1-4]: "); for(i=0; i<4; i++) printf("\t%2.3fA", info.motorCurrents[i]); printf("\n\t[5-8]: "); for(i=0; i<4; i++) printf("\t%2.3fA", info.motorCurrents[i+4]); } else printf("\nError reading motor currents\n"); printf("\n"); if(readBoardVoltages(fd, &info) == SB_OK && readBatteryVoltages(fd, &info) == SB_OK && readBatteryCurrents(fd, &info) == SB_OK) { printf("\nPower information:\n"); printf("\t5V Bus :\t% 2.3fV\t % 2.3fA\n", info.v5VBus, info.i5VBus); printf("\t12V Bus:\t%-2.3fV\t % 2.3fA\n", info.v12VBus, info.i12VBus); printf("\t26V Bus:\t% 2.3fV (Not implemented)\n", info.v26VBus); printf("\tAux Current:\t %2.3fA\n", info.iAux); if(bStat == -1 || bEnable == -1) printf("Could not get enough information about batteries\n"); else { printf("\nBattery information:\n"); unsigned char enableFlags[]= { BATT1_ENABLED, BATT2_ENABLED, BATT3_ENABLED, BATT4_ENABLED, BATT5_ENABLED, BATT6_ENABLED }; unsigned char useFlags[]= { BATT1_INUSE, BATT2_INUSE, BATT3_INUSE, BATT4_INUSE, BATT5_INUSE, BATT6_INUSE }; int i; printf("\tBAT\t Voltage\t Current EN?\tUsed?\n"); for(i=0; i<6; i++) { printf("\t"); if(i != 4) printf("B%d ", i+1); else printf("EXT "); printf("\t% 2.3fV\t% 2.3fA\t ", info.battVoltages[i], info.battCurrents[i]); printf(" %c\t %c", bEnable & enableFlags[i] ? 'Y' : 'N', bStat & useFlags[i] ? 'Y' : 'N'); printf("\n"); } } } else printf("\nError reading power information\n"); } else if(strcmp(argv[1], "-check") == 0) { int ret = pingBoard(fd); if(ret == SB_OK) printf("Ping: OK\n"); else printf("Ping: Error (Code %d)\n", ret); ret = checkBoard(fd); if(ret == SB_OK) printf("Self-test: OK\n"); else printf("Self-test: Error (Code %d)\n", ret); close(fd); return 0; } else if(strcmp(argv[1], "-marker") == 0) { if(argc == 3) { int t = atoi(argv[2]); if(t != 1 && t != 2) { printf("Bad marker number: %d\n", t); close(fd); return -1; } dropMarker(fd, t-1); } else { printf("Which marker? Specify 1 or 2.\n"); } close(fd); return 0; } else if(strcmp(argv[1], "-safe") == 0) { int i; unsigned int cmdList[]= { CMD_THRUSTER1_OFF, CMD_THRUSTER2_OFF, CMD_THRUSTER3_OFF, CMD_THRUSTER4_OFF, CMD_THRUSTER5_OFF, CMD_THRUSTER6_OFF }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 6) { printf("Bad thruster number: %d\n", t); close(fd); return -1; } thrusterCmd(fd, cmdList[t-1]); } else { for(i=0; i<6; i++) thrusterCmd(fd, cmdList[i]); } close(fd); return 0; } else if(strcmp(argv[1], "-battoff") == 0) { unsigned int cmdList[]= { CMD_BATT1_OFF, CMD_BATT2_OFF, CMD_BATT3_OFF, CMD_BATT4_OFF, CMD_BATT5_OFF }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 5) { printf("Bad battery number: %d\n", t); close(fd); return -1; } setBatteryState(fd, cmdList[t-1]); } else { printf("Battery number not specified.\n"); } close(fd); return 0; } else if(strcmp(argv[1], "-batton") == 0) { unsigned int cmdList[]= { CMD_BATT1_ON, CMD_BATT2_ON, CMD_BATT3_ON, CMD_BATT4_ON, CMD_BATT5_ON }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 5) { printf("Bad battery number: %d\n", t); close(fd); return -1; } setBatteryState(fd, cmdList[t-1]); } else { printf("Battery number not specified.\n"); } close(fd); return 0; } else if(strcmp(argv[1], "-unsafe") == 0) { int i; unsigned int cmdList[]= { CMD_THRUSTER1_ON, CMD_THRUSTER2_ON, CMD_THRUSTER3_ON, CMD_THRUSTER4_ON, CMD_THRUSTER5_ON, CMD_THRUSTER6_ON }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 6) { printf("Bad thruster number: %d\n", t); close(fd); return -1; } thrusterCmd(fd, cmdList[t-1]); } else { for(i=0; i<6; i++) thrusterCmd(fd, cmdList[i]); } close(fd); return 0; } else if(strcmp(argv[1], "-baroff") == 0) { int i; unsigned int cmdList[]= { CMD_BAR1_OFF, CMD_BAR2_OFF, CMD_BAR3_OFF, CMD_BAR4_OFF, CMD_BAR5_OFF, CMD_BAR6_OFF, CMD_BAR7_OFF, CMD_BAR8_OFF }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 8) { printf("Bad bar number: %d\n", t); close(fd); return -1; } barCmd(fd, cmdList[t-1]); } else { for(i=0; i<8; i++) barCmd(fd, cmdList[i]); } close(fd); return 0; } else if(strcmp(argv[1], "-baron") == 0) { int i; unsigned int cmdList[]= { CMD_BAR1_ON, CMD_BAR2_ON, CMD_BAR3_ON, CMD_BAR4_ON, CMD_BAR5_ON, CMD_BAR6_ON, CMD_BAR7_ON, CMD_BAR8_ON }; if(argc == 3) { int t = atoi(argv[2]); if(t < 1 || t > 8) { printf("Bad bar number: %d\n", t); close(fd); return -1; } barCmd(fd, cmdList[t-1]); } else { for(i=0; i<8; i++) barCmd(fd, cmdList[i]); } close(fd); return 0; } else if(strcmp(argv[1], "-setbars") == 0) { if(argc == 1) { printf("Segmentation fault\n"); close(fd); return -1; } int t = atoi(argv[2]); setBarOutputs(fd, t); close(fd); return 0; } else if(strcmp(argv[1], "-noblink") == 0) { setAnimation(fd, ANIMATION_NONE); close(fd); return 0; } else if(strcmp(argv[1], "-redblue") == 0) { setAnimation(fd, ANIMATION_REDBLUE); close(fd); return 0; } else if(strcmp(argv[1], "-redgreen") == 0) { setAnimation(fd, ANIMATION_REDGREEN); close(fd); return 0; } else if(strcmp(argv[1], "-tstop") == 0) { setSpeeds(fd, 0, 0, 0, 0, 0, 0); close(fd); return 0; } else if(strcmp(argv[1], "-hardkill") == 0) { hardKill(fd); close(fd); return 0; } else if(strcmp(argv[1], "-diagon") == 0) { setDiagnostics(fd, 1); close(fd); return 0; } else if(strcmp(argv[1], "-diagoff") == 0) { setDiagnostics(fd, 0); close(fd); return 0; } else if(strcmp(argv[1], "-bloff") == 0) { lcdBacklight(fd, LCD_BL_OFF); close(fd); return 0; } else if(strcmp(argv[1], "-blon") == 0) { lcdBacklight(fd, LCD_BL_ON); close(fd); return 0; } else if(strcmp(argv[1], "-blfl") == 0) { lcdBacklight(fd, LCD_BL_FLASH); close(fd); return 0; } else if(strcmp(argv[1], "-intpower") == 0) { switchToInternalPower(fd); close(fd); return 0; } else if(strcmp(argv[1], "-extpower") == 0) { switchToExternalPower(fd); close(fd); return 0; } else if(strcmp(argv[1], "-c") == 0) { displayText(fd, 0, ""); displayText(fd, 1, ""); close(fd); return 0; } else if(strcmp(argv[1], "-s") == 0) { displayText(fd, 0, "Please attach "); displayText(fd, 1, "Start magnet "); while((readStatus(fd) & STATUS_STARTSW) == 0) { usleep(100*1000); } displayText(fd, 0, "Ready to start "); displayText(fd, 1, ""); while((readStatus(fd) & STATUS_STARTSW)) { usleep(100*1000); } displayText(fd, 0, "Running..."); close(fd); return 0; } else if(strcmp(argv[1], "-t") == 0) { displayText(fd, 0, argv[2]); if(argc == 4) displayText(fd, 1, argv[3]); } else if(strcmp(argv[1], "-dvlon") == 0) { int ret; if((ret = DVLOn(fd, 1)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-dvloff") == 0) { int ret; if((ret = DVLOn(fd, 0)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-magpwron") == 0) { int ret; if((ret = setMagPower(fd, 1)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-magpwroff") == 0) { int ret; if((ret = setMagPower(fd, 0)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-firetorp") == 0) { int ret; if(argc != 3) { printf("I need a torpedo number!\n"); } else if((ret = fireTorpedo(fd, atoi(argv[2]))) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-armtorp") == 0) { int ret; if(argc != 3) { printf("I need a torpedo number!\n"); } else if((ret = armTorpedo(fd, atoi(argv[2]))) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-voidtorp") == 0) { int ret; if(argc != 3) { printf("I need a torpedo number!\n"); } else if((ret = voidTorpedo(fd, atoi(argv[2]))) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-extgrabber") == 0) { int ret; if((ret = extendGrabber(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-retgrabber") == 0) { int ret; if((ret = retractGrabber(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-voidgrabber") == 0) { int ret; if((ret = voidGrabber(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-voidall") == 0) { int ret; if((ret = voidSystem(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-pneuoff") == 0) { int ret; if((ret = pneumaticsOff(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[1], "-derpypwr") == 0) { int ret; if(argc != 3) { printf("No parameter for -derpypwr!\n"); } else if(strcmp(argv[2], "on") == 0) { if((ret = setDerpyPower(fd, 1)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else if(strcmp(argv[2], "off") == 0) { if((ret = setDerpyPower(fd, 0)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else { printf("Invalid parameter for -derpypwr!\n"); } } else if(strcmp(argv[1], "-setderpy") == 0) { int ret; if(argc != 3) { printf("No parameter for -setderpy!\n"); } else { if((ret = setDerpySpeed(fd, atoi(argv[2]))) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } } else if(strcmp(argv[1], "-stopderpy") == 0) { int ret; if((ret = stopDerpy(fd)) != SB_OK) printf("Error: %s\n", sbErrorToText(ret)); } else { printf("error: too much bullshit: %s\n", argv[1]); return -1; } close(fd); return 0; }
/*function the present the page- SET BOARD- and handle the events releated to him*/ int setBoard(SDL_Surface* screen){ SDL_Surface* setBoard_Screen = NULL; board_t tmp_board; board_t tmp_board_to_add; location loc; SDL_Event event; pixle pix; int x, y; int check_Board = TRUE; int cnt_op_save_illegal = 0; int show_illegal_sign=FALSE; piece player_to_add; int move_next_page=FALSE; int remove = 0; copyStoS(tmp_board, state->board); // do the adding to the tmp board- in the end- if changed == TRUE- copy the tmp board to the state->board. else- do nothing setBoard_Screen = SDL_LoadBMP("Images/setboard.bmp"); if (setBoard_Screen == NULL){ return 1; } apply_surface(0, 0, setBoard_Screen, screen); fromStateToGui(screen,tmp_board,SET_BOARD); //converts the board(2nd paramter) to graphical form. the 3rd argumant is for internal use //Update Screen SDL_Flip(screen); //we do all the actions on a tmp board for the case we cancel. only after saving the game(and verifing the board is legal)- we saving all the changes in the real board player_to_add.type = EMPTY; while (quit == FALSE && move_next_page == FALSE){ if (SDL_PollEvent(&event)){ if (event.type == SDL_MOUSEBUTTONUP){ if (cnt_op_save_illegal == 1){ show_illegal_sign = 0; cnt_op_save_illegal = 0; check_Board = TRUE; } //Get the mouse offsets x = event.button.x; y = event.button.y; pix.x = x; pix.y = y; if (x <= 600 && y <= 600){// the user clicked on the game Area: // adding a new piece to the board seleted by the user: if (player_to_add.type != EMPTY){ loc = pixel_to_loc(pix); copyStoS(tmp_board_to_add, tmp_board); tmp_board_to_add[loc.column][loc.row].color = player_to_add.color; tmp_board_to_add[loc.column][loc.row].type = player_to_add.type; copyStoS(tmp_board, tmp_board_to_add); player_to_add.type = EMPTY; } if (remove){ //removing a piece selected by user; loc = pixel_to_loc(pix); tmp_board[loc.column][loc.row].color = EMPTY; tmp_board[loc.column][loc.row].type = EMPTY; remove = 0; } //update the board: apply_surface(0, 0, setBoard_Screen, screen); fromStateToGui(screen, tmp_board, SET_BOARD); SDL_Flip(screen); } //add button: if ((x > 624) && (x < 624 + 155) && (y > 76) && (y < 76 + 70)){ remove = 0; player_to_add = select_piece(screen);//selecting piece to add apply_surface(0, 0, setBoard_Screen, screen); // return to the game Area screen fromStateToGui(screen, tmp_board, SET_BOARD); SDL_Flip(screen); if (player_to_add.type == EMPTY){ continue; } } //remove button: if ((x > 624) && (x < 624 + 155) && (y > 76 + 121) && (y < 76 + 121 + 70)){ remove = 1; add_tiles_to_removing_spots(screen, tmp_board); SDL_Flip(screen); } //save: if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 2) && (y < 76 + 121 * 2 + 70)){ remove = 0; check_Board = checkBoard(tmp_board);// checking if the board is legal if (check_Board == TRUE){ // if legal - do the changing on the real board copyStoS(state->board, tmp_board); break; } else{ cnt_op_save_illegal++; show_illegal_sign = 1; } // else- tell the user that the board is illegal and continue editing until the board is legal } //cancel: if ((x > 624) && (x < 624 + 155) && (y > 76 + 121 * 3) && (y < 76 + 121 * 3 + 70)){ break; } //discard changes and return to previous page if (DEBUG){ if ((x > 790) && (y > 590)){ // clear the board button- only for checks and debug remove = 0; clear_board(tmp_board); //update the board: apply_surface(0, 0, setBoard_Screen, screen); fromStateToGui(screen, tmp_board, SET_BOARD); SDL_Flip(screen); } } if (check_Board == FALSE && show_illegal_sign){ add_illegal_sign(screen); SDL_Flip(screen); } }//end of handle key pressed if (event.type == SDL_QUIT){ SDL_FreeSurface(setBoard_Screen); return 1; } } } //Free the loaded image SDL_FreeSurface(setBoard_Screen); return 0; }
int main(int argc, char* args[]) { Screen screen = {}; screen.width = SQUARE_DIM*SCALE*COLUMNS; screen.height = SQUARE_DIM*SCALE*ROWS; //init everything (window, main surface, renderer, SDL_Image) //NOTE: This is probably a bad way to do this since I am changing data inside function instead of returning something if(!init(&screen)) { printf("Something could not be initialized! Error: %s\n", SDL_GetError()); } //set Texture class render pointer Texture::setRenderer(screen.renderer); //set Square's main texture Square::setTexture("tiles.png"); //create a blockbuilder BlockBuilder builder; //get a block pointer Block* block = null; //create an array for the Tetris board that will hold all inanimate squares Board board = {0}; //int numGrid = ROWS*COLUMNS; //Square board_squares[ROWS*COLUMNS]; //set up locations for board squares board.numSquares = COLUMNS*ROWS; for(int i = 0; i < board.numSquares; ++i) { int x = (i%COLUMNS)*SQUARE_DIM*SCALE; int y = (i/COLUMNS)*SQUARE_DIM*SCALE; board.squares[i].setX(x); board.squares[i].setY(y); board.squares[i].concrete = 0; } //create vector for blocks //std::vector<Block*> blocks; //blocks.push_back(builder.buildBlock(L_BLOCK, RED, 0, 0)); bool quit = false; SDL_Event event; //timer variables uint32 currentTime = SDL_GetTicks(); uint32 previousTime = 0; //init RNG srand(time(0)); Color colors[4]; colors[0] = RED; colors[1] = BLUE; colors[2] = GREEN; colors[3] = YELLOW; BlockType blockTypes[5]; blockTypes[0] = T_BLOCK; blockTypes[1] = I_BLOCK; blockTypes[2] = O_BLOCK; blockTypes[3] = S_BLOCK; blockTypes[4] = L_BLOCK; while(!quit) { //check to see if there is no active block if(!block) { int color = rand()%4; int blockType = rand()%5; block = builder.buildBlock(blockTypes[blockType], colors[color], 210, 30); block->setLowestPoint(board.blocksInColumn); //block->setLowestBlockY(); } while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { quit = true; printf("QUITTING! Another successful run! :)\n"); } else if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_UP: { //rotate block counter - clockwise block->rotateBlock(board.squares, board.blocksInColumn, -1); } break; case SDLK_DOWN: { } break; case SDLK_RIGHT: { block->moveBlock(VELOCITY, board.squares, board.blocksInColumn); } break; case SDLK_LEFT: { block->moveBlock(-VELOCITY, board.squares, board.blocksInColumn); } break; case SDLK_SPACE: { //first set block's squares to resting place height block->setToRest(); } break; default: { } } } } //clear screen set to light gray SDL_SetRenderDrawColor(screen.renderer, 0, 0, 0, 0); SDL_RenderClear(screen.renderer); //render the grid renderGrid(screen.renderer); //the block block->render(); //printf("Final Y: %d, LowY: %d\n", block->finalY, block->blockLowY); //render board for(int i = 0; i < board.numSquares; ++i) { if(!board.squares[i].concrete) //check if null { continue; } board.squares[i].renderBoardSquare(); } //printf("x: %d y: %d\n", block->getX(), block->getY()); SDL_RenderPresent(screen.renderer); //bool makeStatic = false; // has block reached its final resting place? //check to see if 1/2 of a second has passed. //if so, update locationsj currentTime = SDL_GetTicks(); if((currentTime - previousTime) > 500) { //printf("currentTime - previousTime = %d\n", currentTime - previousTime); //updateBlocks(&blocks, board_squares); if(updateBlocks(block, &board)) { block = null; //check to see if any rows are full FilledRowRegion check = checkBoard(&board); if(check.firstFilledRow > -1) //then there is a row that is filled! { clearBoard(check.firstFilledRow, check.lastFilledRow, &board); } } previousTime = currentTime; } } SDL_DestroyRenderer(screen.renderer); SDL_DestroyWindow(screen.window); screen.window = null; SDL_Quit(); IMG_Quit(); return 0; }
runState_t Cturn(state_t st, pKind_t player) { if((player==NONE)||(player > BK)) myError("undefined player\n"); printBoard(st); printf("TURN B : CPU thinking\n"); st->player = player; point_t p = allocPoint(); direction_t d; int dx, dy; switch(player) { case A: dx = -1; break; case B: dx = 1; break; default: return 1; } checkBoard(st); if(st->activePiece == 0) return finish; initPieceList(st); minmax(st, DEPTH); p->x = st->piece[st->valueIndex]->p.x; p->y = st->piece[st->valueIndex]->p.y; d = st->valueDirection; st->pieceToMove = allocPiece(); st->pieceToMove->p.x = p->x; st->pieceToMove->p.y = p->y; if(d == L) { st->pieceToMove->left = 1; st->pieceToMove->right = 0; } else if(d == R) { st->pieceToMove->left = 0; st->pieceToMove->right = 1; } switch(d) { case L: dy = -1; break; case R: dy = 1; break; default: return 1; } moveForMinMax(st, p, d); if(st->attack == 1) { //多段攻撃の可能性を考える struct point pa; pa.x = p->x + dx + dx; pa.y = p->y + dy + dy; while(checkPieceToAttack(st, pa)) { //多段攻撃が発生 st->attack = 2; minmax(st, DEPTH); p->x = pa.x; p->y = pa.y; d = st->valueDirection; //st->pieceToMove = allocPiece(); st->pieceToMove->p.x = p->x; st->pieceToMove->p.y = p->y; if(d == L) { st->pieceToMove->left = 1; st->pieceToMove->right = 0; } else if(d == R) { st->pieceToMove->left = 0; st->pieceToMove->right = 1; } moveForMinMax(st, p, d); switch(d) { case L: dy = -1; break; case R: dy = 1; break; default: return 1; } pa.x = p->x + dx + dx; pa.y = p->y + dy + dy; } } return cont; }
double ChessAI::findBestMove(bool playIt, int iter) { PieceColor color=colorToMove; Square s, e; MoveHolder moves(settings.checkWidth); if (checkForBoringTie()) { //err << "\n\n\n BORING TIE FOUND\n\n\n" << err; return 0.5; } for (int i=((color==WHITE)?0:16); i<((color==WHITE)?16:32); ++i) { if (pieces[i].alive) { s=pieces[i].square; for (e.y=0; e.y<8; ++e.y) { for (e.x=0; e.x<8; ++e.x) { bool path=checkMovePath(s, e); bool check=checkCheck(colorToMove, s, e); if (path && check) { forceMove(s, e); moves.add(MoveData(s, e, eval(color))); undo(); checkBoard(); } } } } } if (moves.size()<1) { if (checkCheck(color, kings[color]->square, kings[color]->square)) return 0.5; else return 0.0; } MoveData bestMove; bestMove.score=-1; for (auto i=moves.begin(); i!=moves.end(); ++i) { for (int j=settings.checkDepth; j>iter; --j) err << " "; err << pieceColor2Name(color) << " " << pieceType2Name(board((*i).s)->type) << " from " << (*i).s.str() << " to " << (*i).e.str() << err; forceMove((*i).s, (*i).e); double score=0; if (iter<=0) { score=eval(color); } else { score=1-findBestMove(false, iter-1); } undo(); for (int j=settings.checkDepth+1; j>iter; --j) err << " "; err << "best score for " << pieceColor2Name(myColor) << ": " << ((myColor==color)?score:1-score) << err; if (score>bestMove.score) { bestMove.s=(*i).s; bestMove.e=(*i).e; bestMove.score=score; } } if (playIt) { return game->playMove(bestMove.s, bestMove.e); } else { return bestMove.score; } }
int SqAttacked(const int sq, const int side, const S_BOARD *pos) { int piece; int tmpSQ; int dir; int i; ASSERT(SqOnBoard(sq)); ASSERT(SideValid(side)); ASSERT(checkBoard(pos)); //pawn attacks if(side == WHITE) { if(pos->pieces[sq-11] == wP || pos->pieces[sq-9] == wP) return TRUE; } else { //BLACK if(pos->pieces[sq+11] == bP || pos->pieces[sq+9] == bP) return TRUE; } //knight attacks for(i = 0; i < 8; i++) { piece = pos->pieces[ sq + KnDir[i] ]; if(piece != OFFBOARD && IsKn(piece) && PieceColor[piece]==side) return TRUE; } //king attacks for(i = 0; i < 8; i++) { piece = pos->pieces[ sq + KiDir[i] ]; if(piece != OFFBOARD && IsKi(piece) && PieceColor[piece]==side) { return TRUE; } } //rook and Queen attacks for(i = 0; i < 4; i++) { dir = RkDir[i]; tmpSQ = sq + dir; piece = pos->pieces[tmpSQ]; while(piece != OFFBOARD) { if(piece != EMPTY) { if(piece != OFFBOARD && IsRQ(piece) && PieceColor[piece] == side) return TRUE; break; } tmpSQ += dir; piece = pos->pieces[tmpSQ]; } } //bishop and queen attacks for(i = 0; i < 4; i++) { dir = BiDir[i]; tmpSQ = sq + dir; piece = pos->pieces[tmpSQ]; while(piece != OFFBOARD) { if(piece != EMPTY) { if(piece != OFFBOARD && IsBQ(piece) && PieceColor[piece] == side) return TRUE; break; } tmpSQ += dir; piece = pos->pieces[tmpSQ]; } } return FALSE; }
/* * the method Solve looks for a word, then see if such word appears on the board. * and repeats for every word in the dictionary; * this method is faster than looking the board first then see if the word is in the dictionary * */ void Boggle::Solve( const char * a_Grid, std::vector< std::string > & a_Results ) const { int gridsize = (int) sqrt( (double) sizeof(a_Grid)); // only squared matrix int x = GetSize(); int wordCount = 0; int i, j, k, l; char board[MAX_BOARD_SIZE + 1][MAX_BOARD_SIZE + 2]= {{'\0'}}; bool visited[MAX_BOARD_SIZE][MAX_BOARD_SIZE] = {{false}}; for(i = 0; i < gridsize; ++i){ // here we initialize the board with given values for (j = 0; j < gridsize; ++j){ board[i][j] = a_Grid[gridsize * i + j]; } } int pLen = 0; std::string currentWord; std::ifstream myfile (mDictionary); if (myfile.is_open()) { while ( getline (myfile,currentWord) ) // we will look into the dictionary, word by word, { int len = currentWord.length(); if (currentWord[len-1] == '\n') { currentWord[len-1] = '\0'; // remove the trailing newline --len; } if (len < MIN_WORD_SIZE) continue; // we only want words longer than ... for(i = 0; i < gridsize; ++i) { for(j = 0; j < gridsize; ++j) { // clear the visited array for(int k = 0; k < MAX_BOARD_SIZE; ++k){ for (int l = 0; l < MAX_BOARD_SIZE; ++l){ visited[k][l] = false; } } // start looking for word if ( checkBoard(currentWord,board,visited, 0, i, j) ) a_Results.push_back(currentWord); } } } myfile.close(); } return; }