c++ tictactoe [message #1549924] |
Tue, 06 January 2015 20:36 |
asdfgh hgfdsa Messages: 2 Registered: January 2015 |
Junior Member |
|
|
Hey guys. I´m working on a project for my school to create an tictactoe game against computer. I´ve wrote a function, which generate a move but it doesent works correctly. It starts to set his 'O' after I set my second X. I can´t find the mistake. Please help.
#include "Header.h"
int main()
{
srand(time(NULL));
Ausgabe();
while (true)
{
Eingabe();
Ausgabe();
ComputerZug();
}
system("pause");
return 0;
}
#include <iostream>
#include <ctype.h>
#include <time.h>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };//Matrix mit 3mal3 Feldern, die mit Zahl 1 - 9 gefühlt werden
char spieler = 'X'; //Zeichen für den Spieler
char computer = 'O'; //Zeichen für Computer
int i, j;
void Ausgabe()//Funktion für Das Spielbrett
{
system("cls");//Jedes mal, wenn das Spielbrett neu ausgegeben wird, werden die vorigen Inhalte gelöscht
cout << "\t\t\t" << "\n\n\nPraktikum 6 Tic Tac Toe Proejct.\n\n\n";
cout << "\t\t\t" << ".---.---.---." << endl;
cout << "\t\t\t" << "| " << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << " | " << endl;//erste Zeile
cout << "\t\t\t" << ":---+---+---:" << endl;
cout << "\t\t\t" << "| " << matrix[1][0] << " | " << matrix[1][1] << " | " << matrix[1][2] << " | " << endl;//zweiteZeil
cout << "\t\t\t" << ":---+---+---:" << endl;
cout << "\t\t\t" << "| " << matrix[2][0] << " | " << matrix[2][1] << " | " << matrix[2][2] << " | " << endl;//dritte Zeile
cout << "\t\t\t" << " --- --- --- \n\n\n";
}
void Eingabe()//Dammit beim drücken der 1, auch das X in dem Kästchen mit der 1 erscheint. Sonst würde er bei der 2 rauskommen
{
int a;
cout << "Drücke die entsprechende Zahl auf der Tastatur für die Eingabe: ";
cin >> a;
if (a == 1)
{
matrix[0][0] = spieler;
}
else if (a == 2)
matrix[0][1] = spieler;
else if (a == 3)
matrix[0][2] = spieler;
else if (a == 4)
matrix[1][0] = spieler;
else if (a == 5)
matrix[1][1] = spieler;
else if (a == 6)
matrix[1][2] = spieler;
else if (a == 7)
matrix[2][0] = spieler;
else if (a ==
matrix[2][1] = spieler;
else if (a == 9)
matrix[2][2] = spieler;
}
void ComputerZug()
{
srand(time(NULL));
int AI;
AI = 0;
while (AI == 0)
{
i = rand() % 3;
j = rand() % 3;
if ((matrix[i][j] == 'X') || (matrix[i][j] == 'O'))
{
AI = 0;
}
else
{
matrix[i][j] = 'O';
AI++;
}
}
}
|
|
|
Re: c++ tictactoe [message #1559023 is a reply to message #1549924] |
Sun, 11 January 2015 21:16 |
|
Try to add Ausgabe() after ComputerZug() within main(). Otherwise you will not see what the computer has done.
Your computer has no strategy to win. Shall it remain so stupid?
Niko
Eclipse-CDT Version: 2019-12 (4.14.0), Win10 64bit
|
|
|
Powered by
FUDForum. Page generated in 0.03337 seconds