void play(char *position) { pieces = initPieces(position); char player = 'w'; int from, to; int legal; while(1) { if(isMate(pieces, player)) return; char *posString = createPosString(pieces); printBoard(posString); free(posString); from = -1; to = -1; legal = 0; printMoves(pieces, player); while(!legal) { readMove(&from, &to); legal = legalMove(pieces, from, to, player); char *fromSt, *toSt; if(!legal) { printf("%s %s is illegal\n", fromSt = stringPosToSquare(from), toSt = stringPosToSquare(to)); free(fromSt); free(toSt); } } movePiece(pieces, from, to); player = player == 'w' ? 'b' : 'w'; } }
bool Board::canMove(SquareState player) { if (player == EMPTY) { std::string error = "ReversiBoard::canMove : Empty square is not a player."; throw error; } for (int x = 0; x < tSize; ++x) { for (int y = 0; y < tSize; ++y) { if (legalMove(x, y, player)) { return true; } } } return false; }
void Board::turn(SquareState player, Individual* individual) { if (player == EMPTY) { std::string error = "ReversiBoard::turn : Empty square is not a player."; throw error; } float maxQuality = 0; vector<Move> moves; for (int x = 0; x < tSize; ++x) { for (int y = 0; y < tSize; ++y) { if (legalMove(x, y, player)) { Move move; move.xPos = x; move.yPos = y; if (individual == NULL) { move.quality = computerEstimation(x, y, player); } else { move.quality = individualEstimation(x, y, player, individual); } if (move.quality >= maxQuality || moves.size() == 0) { maxQuality = move.quality; moves.push_back(move); } } } } vector<Move> bestMoves; for (int i = 0; i < moves.size(); ++i) { if (moves[i].quality == maxQuality) { bestMoves.push_back(moves[i]); } } if (bestMoves.size() > 0) { Move chosenMove = bestMoves[Random::positiveInteger(bestMoves.size())]; makeMove(chosenMove.xPos, chosenMove.yPos, player); } }
int do_eco(const string &eco_line) { ColorType side = White; Board board; int movecount = 0; stringstream s(eco_line); const string &str = s.str(); string::const_iterator it = str.begin(); // Follow the opening line string token; string code; string name; int first_token = 1; while (it != str.end()) { // skip spaces while (isspace(*it) && it != str.end()) it++; if (it == str.end()) break; // extract text string text; int first = 1; int quoted = 0; while (it != str.end() && (quoted ? (*it != '"') : !isspace(*it))) { if (first && *it == '"') { quoted++; it++; first = 0; continue; } text += *it++; } if (first_token) { code = text; first_token = 0; continue; } else if (quoted) { name = text; break; } else if (text.length() == 0) { break; } // skip numbers if (isdigit(text[0])) continue; if (!isalpha(text[0])) return -1; // parse the move Move m = Notation::value(board,side,Notation::SAN_IN,text); if (IsNull(m) || !legalMove(board,StartSquare(m),DestSquare(m))) { cerr << "Illegal or invalid move: " << text << endl; return -1; } else { ++movecount; board.doMove(m); } side = OppositeColor(side); } if (code.length()) { cout << '{' << '"' << code << '"' << ", "; write_64(board.hashCode(),cout); cout << " ,"; if (name.length()) cout << '"' << name << '"'; else cout << '"' << '"'; cout << "},"; cout << endl; } return 0; }