Forum: luffar-schack

Forum huvudsida -> Programmering -> luffar-schack

Sidor: 1

Till botten

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:

  1. //tic-tac-toe
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. //global constants
  10. const char X = ''X'';
  11. const char O = ''O'';
  12. const char EMPTY = '' '';
  13. const char TIE = ''T'';
  14. const char NO_ONE = ''N'';
  15.  
  16. //functions prototype
  17. void instructions();
  18. char asYesNo(string question);
  19. int askNumber(string question, int high, int low = 0);
  20. char humanPiece();
  21. char opponent(char piece);
  22. void displayBoard(const vector<char>& board);
  23. char winner(const vector<char>& board);
  24. bool isLegal(const vector<char>& board, int move);
  25. int humanMove(const vector<char>& board, char human);
  26. int computerMove(vector<char> board, char computer);
  27. void announceWinner(char winner, char computer, char human);
  28.  
  29.  
  30. int main()
  31. {
  32. int move;
  33. const int NUM_SQUARES = 9;
  34. vector<char> board(NUM_SQUARES, EMPTY);
  35.  
  36. instructions();
  37. char human = humanPiece();
  38. char computer = opponent(human);
  39. char turn = X;
  40. displayBoard(board);
  41.  
  42. while (winner(board) == NO_ONE)
  43. {
  44. if (turn == human)
  45. {
  46. move = humanMove(board, human);
  47. board[move] = human;
  48. }
  49. else
  50. {
  51. move = computerMove(board, computer);
  52. board[move] = computer;
  53. }
  54. displayBoard(board);
  55. turn = opponent(turn);
  56. }
  57.  
  58. announceWinner(winner(board), computer, human);
  59.  
  60. cin.get();
  61. return 0;
  62. }
  63.  
  64. void instructions()
  65. {
  66. cout << "Welcome to the ultimate man VS machine showdown: tic-tac-toe!!.n";
  67. cout << "Where human brain is pit against silicon processornn";
  68.  
  69. cout << "Make your move by entering a number, 0-8. The numbern";
  70. cout << "corresponds to the desired board position, as illustrated:nn";
  71.  
  72. cout << " 0 | 1 | 2n";
  73. cout << " ---------n";
  74. cout << " 3 | 4 | 5n";
  75. cout << " ---------n";
  76. cout << " 6 | 7 | 8nn";
  77.  
  78. cout << "Prepare yourself, human, the battle is about to begin.nn";
  79. }
  80.  
  81. char askYesNo(string question)
  82. {
  83. char response;
  84. do
  85. {
  86. cout << question << " (y/n): ";
  87. cin >> response;
  88. } while (response != ''y'' && response != ''n'');
  89.  
  90. return response;
  91. }
  92.  
  93.  
  94. int askNumber(string question, int high, int low)
  95. {
  96. int number;
  97. do
  98. {
  99. cout << question << " (" << low << " - " << high << "): ";
  100. cin >> number;
  101. }while (number > high || number < low);
  102.  
  103. return number;
  104. }
  105.  
  106.  
  107. char humanPiece()
  108. {
  109. char go_first = askYesNo("Do you require the first move?");
  110. if (go_first == ''y'')
  111. {
  112. cout << "nThen take the first move you will need it.n";
  113. return X;
  114. }
  115. else
  116. {
  117. cout << "nYour bravery will be your undoing... I will go first.n";
  118. return 0;
  119. }
  120. }
  121.  
  122.  
  123. char opponent(char piece)
  124. {
  125. if (piece == X)
  126. return 0;
  127. else
  128. return X;
  129. }
  130.  
  131.  
  132. void displayBoard(const vector<char>& board)
  133. {
  134. cout << "nt" << board[0] << " | " << board[1] << " | " << board[2];
  135. cout << "nt" << "----------";
  136. cout << "nt" << board[3] << " | " << board[4] << " | " << board[5];
  137. cout << "nt" << "----------";
  138. cout << "nt" << board[6] << " | " << board[7] << " | " << board[8];
  139. cout << "nn";
  140. }
  141.  
  142.  
  143. char winner(const vector<char>& board)
  144. {
  145. //all possible winning rows
  146. const int WINNING_ROWS[8][3]={ {0, 1, 2},
  147. {3, 4, 5},
  148. {6, 7, 8},
  149. {0, 3, 6},
  150. {1, 4, 7},
  151. {2, 5, 8},
  152. {0, 4, 8},
  153. {2, 4, 6} };
  154.  
  155. const int TOTAL_ROWS = 8;
  156.  
  157. //if any row has three values that are the same(and not empty).
  158. //then we have a winner
  159. for(int row = 0; row < TOTAL_ROWS; ++row)
  160. {
  161. if ( (board[WINNING_ROWS[row][0]] != EMPTY ) &&
  162. (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
  163. (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
  164. {
  165. return board[WINNING_ROWS[row][0]];
  166. }
  167.  
  168. //since nobody has won, check for a tie(no empty squares left)
  169. if (count(board.begin(), board.end(), EMPTY) == 0)
  170. return TIE;
  171.  
  172. //since noone has won and it is not a tie the game isn''t over
  173. return NO_ONE;
  174. }
  175.  
  176. }
  177.  
  178.  
  179. inline bool isLegal(const vector<char>& board, int move)
  180. {
  181. return (board[move] == EMPTY);
  182. }
  183.  
  184.  
  185. int humanMove(const vector<char>& board, char human)
  186. {
  187. int move = askNumber("Where will you move?", (board.size() - 1));
  188. while (!isLegal(board, move))
  189. {
  190. cout << "The square is already occupied, foolish man.n";
  191. move = askNumber("Where will you move?", (board.size() - 1));
  192. }
  193. cout << "Fine...n";
  194. return move;
  195. }
  196.  
  197.  
  198. int computerMove(vector<char> board, char computer)
  199. {
Dread 18:59 - 26:e September 2007 | Post #2
Medlem
Inlägg: 135


Skicka PM
ser väldigt fel ut i slutet med bara en hakparantes, sen om du skriver ut vad den gnäller om så skulle det underlätta ganska mkt.

,,r det möjligen c++ for the absolute beginner du använder? Har nämligen nästan exakt samma exempel fast i python boken Smiley

-------------------------
c++

Senast redigerad 19:01 - 26:e September 2007


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 Smiley.

-------------------------
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
Atom feed

Du får inte posta i den här tråden | Till toppen