summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp')
-rw-r--r--Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp385
1 files changed, 385 insertions, 0 deletions
diff --git a/Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp b/Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp
new file mode 100644
index 0000000..0b9693d
--- /dev/null
+++ b/Bachelor/Prog1/Prakt6/prg1p6_2/tictactoe.cpp
@@ -0,0 +1,385 @@
+// Programmieren 1, Praktikum 6, Aufgabe 1
+// Author: Sven Eisenhauer
+// Date: 14.01.2005
+// File: tictactoe.cpp
+// Description: Implementation of class TicTacToe
+
+#include "tictactoe.h"
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+using std::cout;
+using std::endl;
+using std::cin;
+
+TicTacToe::TicTacToe()
+:nrFields(9)
+{
+ for (int i=0;i<nrFields;i++)
+ field[i]=static_cast <char> (i+49);
+ winner="";
+ round=0;
+}
+
+TicTacToe::~TicTacToe()
+{
+ cout << endl << "Game Over!" << endl;
+}
+
+void TicTacToe::eraseField()
+{
+ for (int i=0;i<nrFields;i++)
+ field[i]=' ';
+}
+
+void TicTacToe::playOneGame()
+{
+ srand(time(0));
+ welcome();
+ print();
+ eraseField();
+ setBeginner();
+ print();
+ while (""==getWinner())
+ {
+ if("Player" == getNext())
+ {
+ movePlayer();
+ }
+ else
+ {
+ moveComputer();
+
+ }
+ print();
+ setWinner();
+ }
+ printWinner();
+}
+
+void TicTacToe::print()
+{
+
+ cout << endl;
+ for (int i=0;i<nrFields;i+=3)
+ {
+ cout << field[i] << " | "<< field[i+1]<< " | "<< field[i+2]<<endl;
+ if (i<6)
+ {
+ for (int n=0;n<10;n++)
+ cout << "-";
+ cout << endl;
+ }
+ }
+ cout << endl;
+}
+
+void TicTacToe::welcome()
+{
+ cout << endl << "TicTacToe!" << endl <<
+ "To mark a field, just enter the according number ('1' means field one)!" << endl;
+}
+
+void TicTacToe::setWinner()
+{
+ int n=0;
+
+ while (n<7 && ""==getWinner())
+ {
+ if (0==(n%3))
+ {
+ if (field[n]=='X' &&field[n+1]=='X' &&field[n+2]=='X')
+ winner="Player";
+ if (field[n]=='O' &&field[n+1]=='O' &&field[n+2]=='O')
+ winner="Computer";
+ }
+ if (n<=2)
+ {
+ if (field[n]=='X' &&field[n+3]=='X' &&field[n+6]=='X')
+ winner="Player";
+ if (field[n]=='O' &&field[n+3]=='O' &&field[n+6]=='O')
+ winner="Computer";
+ }
+ if (n==0)
+ {
+ if (field[n]=='X' &&field[n+4]=='X' &&field[n+8]=='X')
+ winner="Player";
+ if (field[n]=='O' &&field[n+4]=='O' &&field[n+8]=='O')
+ winner="Computer";
+ }
+ if (n==2)
+ {
+ if (field[n]=='X' &&field[n+2]=='X' &&field[n+4]=='X')
+ winner="Player";
+ if (field[n]=='O' &&field[n+2]=='O' &&field[n+4]=='O')
+ winner="Computer";
+ }
+ n++;
+ }
+ if (round==nrFields && getWinner()=="")
+ {
+ winner="Draw";
+ }
+}
+
+void TicTacToe::setBeginner()
+{
+ char input;
+ cout << endl << "Who has first move?" << endl
+ << "[P]layer or [C]omputer: ";
+ do
+ {
+ cin >> input;
+ switch (input)
+ {
+ case 'p':
+ case 'P':
+ nextPlayer="Player";
+ break;
+ case 'c':
+ case 'C':
+ nextPlayer="Computer";
+ break;
+ default:
+ cout << endl << "Bad selection! Enter new selection: ";
+ }
+ } while (input!='P' && input!='C' && input!='p' && input!='c');
+}
+void TicTacToe::movePlayer()
+{
+ int draw=0;
+ cout << endl << "Enter your move: ";
+ //print();
+ cin >> draw;
+ while (testField(draw-1))
+ {
+ cout << endl << "Irregular move... please enter a new one" << endl;
+ cin >> draw;
+ }
+ field[draw-1]='X';
+ setNext("Computer");
+ round++;
+}
+
+int TicTacToe::testField(int f)
+{
+ if (field[f]==' ')
+ return 0;
+ else
+ return 1;
+}
+
+void TicTacToe::moveComputer()
+{
+ int move=4;
+ int vertX[3]={0};
+ int horX[3]={0};
+ int diagX[2]={0};
+ int vertO[3]={0};
+ int horO[3]={0};
+ int diagO[2]={0};
+ int n=0,
+ x=0,
+ y=0,
+ d=0;
+
+ if (testField(move))
+ {
+ while(testField(n) && n<nrFields)
+ {
+ n+=2;
+ }
+ move=n;
+ }
+
+ if (testField(move))
+ {
+ n=1;
+ while(testField(n) && n<nrFields)
+ {
+ n+=2;
+ }
+ move=n;
+ }
+
+ //defense
+ for (n=0;n<3;n++)
+ if (field[n]=='X')
+ vertX[0]++;
+ for (n=3;n<6;n++)
+ if (field[n]=='X')
+ vertX[1]++;
+ for (n=6;n<9;n++)
+ if (field[n]=='X')
+ vertX[2]++;
+
+ for (n=0;n<=6;n+=3)
+ if (field[n]=='X')
+ horX[0]++;
+ for (n=1;n<=7;n+=3)
+ if (field[n]=='X')
+ horX[1]++;
+ for (n=2;n<=8;n+=3)
+ if (field[n]=='X')
+ horX[2]++;
+
+ for (n=0;n<=8;n+=4)
+ if (field[n]=='X')
+ diagX[0]++;
+ for (n=2;n<=6;n+=2)
+ if (field[n]=='X')
+ diagX[1]++;
+
+ // offense
+
+ for (n=0;n<3;n++)
+ if (field[n]=='O')
+ vertO[0]++;
+ for (n=3;n<6;n++)
+ if (field[n]=='O')
+ vertO[1]++;
+ for (n=6;n<9;n++)
+ if (field[n]=='O')
+ vertO[2]++;
+
+ for (n=0;n<=6;n+=3)
+ if (field[n]=='O')
+ horO[0]++;
+ for (n=1;n<=7;n+=3)
+ if (field[n]=='O')
+ horO[1]++;
+ for (n=2;n<=8;n+=3)
+ if (field[n]=='O')
+ horO[2]++;
+
+ for (n=0;n<=8;n+=4)
+ if (field[n]=='O')
+ diagO[0]++;
+ for (n=2;n<=6;n+=2)
+ if (field[n]=='O')
+ diagO[1]++;
+
+
+ for (n=0;n<3;n++)
+ if (vertX[n]==2 && vertO[n]==0)
+ {
+ switch(n) {
+ case 0: x=n;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ case 1: x=n+2;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ case 2: x=n+5;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ }
+ move=x;
+ }
+ for (n=0;n<3;n++)
+ if (horX[n]==2 && horO[n]==0)
+ {
+ switch(n) {
+ case 0: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ case 1: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ case 2: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ }
+ move=y;
+ }
+ for (n=0;n<2;n++)
+ if (diagX[n]==2 && diagO[n]==0)
+ {
+ switch(n) {
+ case 0: d=n;
+ while (testField(d)&& d<nrFields)
+ d+=4;
+ break;
+ case 1: d=2;
+ while (testField(d)&& d<nrFields)
+ d+=2;
+ break;
+ }
+ move=d;
+ }
+ for (n=0;n<3;n++)
+ if (vertO[n]==2 && vertX[n]==0)
+ {
+ switch(n) {
+ case 0: x=n;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ case 1: x=n+2;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ case 2: x=n+5;
+ while (testField(x) && x<nrFields)
+ x++;
+ break;
+ }
+ move=x;
+ }
+ for (n=0;n<3;n++)
+ if (horO[n]==2 && horX[n]==0)
+ {
+ switch(n) {
+ case 0: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ case 1: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ case 2: y=n;
+ while (testField(y)&& y<nrFields)
+ y+=3;
+ break;
+ }
+ move=y;
+ }
+ for (n=0;n<2;n++)
+ if (diagO[n]==2 && diagX[n]==0)
+ {
+ switch(n) {
+ case 0: d=n;
+ while (testField(d)&& d<nrFields)
+ d+=4;
+ break;
+ case 1: d=2;
+ while (testField(d)&& d<nrFields)
+ d+=2;
+ break;
+ }
+ move=d;
+ }
+
+ cout << endl << "Computer moves: ";
+ field[move]='O';
+ //print();
+ setNext("Player");
+ round++;
+}
+
+void TicTacToe::printWinner()
+{
+ system("cls");
+ if (getWinner()!="Draw")
+ cout << getWinner() << " won the match!" << endl;
+ else
+ cout << "Draw Game, no winner!" << endl;
+ print();
+} \ No newline at end of file