Forum huvudsida -> Programmering -> luffar-schack
Sidor: 1
Cpp_skater | 18:46 - 26:e September 2007 | Post #1 | |||||||
Medlem Inlägg: 43 Skicka PM |
tjena allihop!! ja har jobbat med ett luffar-schack ur en bok jag har men har lyckats sabba koden och hittar inte felet men nu hoppas jag att det finns ngn här som kan hjäloa till här är main.cpp:
|
Cpp_skater | 19:10 - 26:e September 2007 | Post #3 | |
Medlem Inlägg: 43 Skicka PM |
faan vf blir inte hela coden med //tic-tac-toe #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; //global constants const char X = ''X''; const char O = ''O''; const char EMPTY = '' ''; const char TIE = ''T''; const char NO_ONE = ''N''; //functions prototype void instructions(); char asYesNo(string question); int askNumber(string question, int high, int low = 0); char humanPiece(); char opponent(char piece); void displayBoard(const vector<char>& board); char winner(const vector<char>& board); bool isLegal(const vector<char>& board, int move); int humanMove(const vector<char>& board, char human); int computerMove(vector<char> board, char computer); void announceWinner(char winner, char computer, char human); int main() { int move; const int NUM_SQUARES = 9; vector<char> board(NUM_SQUARES, EMPTY); instructions(); char human = humanPiece(); char computer = opponent(human); char turn = X; displayBoard(board); while (winner(board) == NO_ONE) { if (turn == human) { move = humanMove(board, human); board[move] = human; } else { move = computerMove(board, computer); board[move] = computer; } displayBoard(board); turn = opponent(turn); } announceWinner(winner(board), computer, human); cin.get(); return 0; } void instructions() { cout << "Welcome to the ultimate man VS machine showdown: tic-tac-toe!!.n"; cout << "Where human brain is pit against silicon processornn"; cout << "Make your move by entering a number, 0-8. The numbern"; cout << "corresponds to the desired board position, as illustrated:nn"; cout << " 0 | 1 | 2n"; cout << " ---------n"; cout << " 3 | 4 | 5n"; cout << " ---------n"; cout << " 6 | 7 | 8nn"; cout << "Prepare yourself, human, the battle is about to begin.nn"; } char askYesNo(string question) { char response; do { cout << question << " (y/n): "; cin >> response; } while (response != ''y'' && response != ''n''); return response; } int askNumber(string question, int high, int low) { int number; do { cout << question << " (" << low << " - " << high << "): "; cin >> number; }while (number > high || number < low); return number; } char humanPiece() { char go_first = askYesNo("Do you require the first move?"); if (go_first == ''y'') { cout << "nThen take the first move you will need it.n"; return X; } else { cout << "nYour bravery will be your undoing... I will go first.n"; return 0; } } char opponent(char piece) { if (piece == X) return 0; else return X; } void displayBoard(const vector<char>& board) { cout << "nt" << board[0] << " | " << board[1] << " | " << board[2]; cout << "nt" << "----------"; cout << "nt" << board[3] << " | " << board[4] << " | " << board[5]; cout << "nt" << "----------"; cout << "nt" << board[6] << " | " << board[7] << " | " << board[8]; cout << "nn"; } char winner(const vector<char>& board) { //all possible winning rows const int WINNING_ROWS[8][3]={ {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} }; const int TOTAL_ROWS = 8; //if any row has three values that are the same(and not empty). //then we have a winner for(int row = 0; row < TOTAL_ROWS; ++row) { if ( (board[WINNING_ROWS[row][0]] != EMPTY ) && (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) && (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) ) { return board[WINNING_ROWS[row][0]]; } //since nobody has won, check for a tie(no empty squares left) if (count(board.begin(), board.end(), EMPTY) == 0) return TIE; //since noone has won and it is not a tie the game isn''t over return NO_ONE; } } inline bool isLegal(const vector<char>& board, int move) { return (board[move] == EMPTY); } int humanMove(const vector<char>& board, char human) { int move = askNumber("Where will you move?", (board.size() - 1)); while (!isLegal(board, move)) { cout << "The square is already occupied, foolish man.n"; move = askNumber("Where will you move?", (board.size() - 1)); } cout << "Fine...n"; return move; } int computerMove(vector<char> board, char computer) { cout << "Hmmm, I shall take square number n"; //if computer can win on next move do it for(int move = 0; move < board.size(); ++move) { if (isLegal(board, move)) { board[move] = computer; if (winner(board) == computer) { cout << move << endl; return move; } //done checking this move undo it board[move] = EMPTY; } } //if the human can win on his next move, block it char human = opponent(computer); for(int move = 0; move < board.size(); ++move) { if (isLegal(board, move)) { board[move] = human; if (winner(board) == human) { cout << move << endl; return move; } //done checking this move, undo it board[move] = EMPTY; } } // no one can win on there next move //the best moves to make, in order const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7}; //pics the best place for(int i = 0; i < board.size(); ++i) { int move = BEST_MOVES[i]; if (isLegal(board, move)) { cout << move << endl; return move; } } } void announceWinner(char winner, char computer, char human) { if (winner == computer) { cout << winner << "''s won!n"; cout << "As i predicted, human, I am a triumphant once more - proofn"; cout << "that computers are superior to humans in all regards.n"; } else if (winner == human) { cout << winner << "''s won!n"; cout << "No, noooo!, it cannot be! somehow you tricked me, human.n"; cout << "but never again! I, the computer, so swear it.n";; } else { cout << "It is a tie.n"; cout << "You were most lucky, human, and somehow managed to tie me.n"; cout << "Celebrate... because this is the best you will ever achieve"; } } o felet var ju då att ''O'' inte visas i på brädet Senast redigerad 19:37 - 26:e September 2007 |
Slash | 22:06 - 26:e September 2007 | Post #4 | |
Medlem Inlägg: 141 Skicka PM |
Skulle gissa att problemet ligger i följande funktion. char opponent(char piece) { if (piece == X) return 0; else return X; } Nej, kolla igen, det är inte ett O . ------------------------- Ingen sigantur! Senast redigerad 22:06 - 26:e September 2007 |
Cpp_skater | 20:22 - 27:e September 2007 | Post #5 | |
Medlem Inlägg: 43 Skicka PM |
ojj det funkar men det är ett annat fel det går inte att vinna!! |
Dread | 20:53 - 27:e September 2007 | Post #6 | |
Medlem Inlägg: 135 Skicka PM |
så vitt jag kan se så har du "bara" return no_one eller/och Tie, dvs du kollar aldrig ifall någon vunnit inte heller vem som har vunnit. vilket iofs inte borde vara för svårt att fixa, bara kolla efter varje drag, och sen beroende på om det är jämt/ojämt så vinner den som började/var tvåa. ------------------------- c++ Senast redigerad 20:54 - 27:e September 2007 |
Cpp_skater | 21:01 - 27:e September 2007 | Post #7 | |
Medlem Inlägg: 43 Skicka PM |
men det är ju {return board[WINNING_ROWS[row][0]]; } som returnar vem som har vunnit |
Sidor: 1
Forum huvudsida -> Programmering -> luffar-schack
Du får inte posta i den här tråden | Till toppen
Datum
2024-05-06 13:07
Vecka: 19
Besökare
Inloggade: 0
Gäster: 605
Medlemmar
Medlemmar: 53576
Forum
Senaste inlägget: HAHA MÄNNISKAN LEVER ÄN (13:45 - 18:e Januari 2019, av Hetzz)
Wiki
Senaste ändringen:
3D (rev: 1, tid: 09:40 - 13:e November 2015)
Chatt
#blinkenlights @ Libera.Chat
Gratis shellkonton för IRC, webbsidor mm.