Tic Tac Toe Minimax problem

Hello please help me with this tic tac toe program.I am trying to implement Minimax in tic tac toe and it is not working.Please correct my code.Also i need
to do this in turbo c++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<iostream.h>
#include<math.h>
#include<string.h>
#include<graphics.h>
#include<bios.h>
#include<dos.h>
#include<stdlib.h>
#include<conio.h>
char board[3][3];
int key=0,g=0;
void startboard()
{
  for(int i=0;i<3;++i)
    {
      for(int j=0;j<3;++j)
	board[i][j]=' ';
    }
}
void draw_board()
{
  rectangle(getmaxx()/2-62,getmaxy()/2-65,getmaxx()/2+60,getmaxy()/2+80);
  //vert
  line(getmaxx()/2-22,getmaxy()/2-64,getmaxx()/2-22,getmaxy()/2+80);
  line(getmaxx()/2+18,getmaxy()/2-64,getmaxx()/2+18,getmaxy()/2+80);
  //horiz
  line(getmaxx()/2-60,getmaxy()/2-14,getmaxx()/2+60,getmaxy()/2-14);
  line(getmaxx()/2-60,getmaxy()/2+30,getmaxx()/2+60,getmaxy()/2+30);


 int m=0,n=0;
  for(int i=35;i<=45;i+=5)
  {
    for(int j=13;j<=19;j+=3)
      {
	gotoxy(i,j);
	cout<<board[n][m];
	++n;
      }
    n=0;
    ++m;
   }
}
void user()
{
  int row,col;
  gotoxy(1,1);
  cout<<"\n\nEnter input row & column:\n";
  cin>>row>>col;
  board[row][col]='X';
}
int check(char ch)
{
  int e=0;
  for(int x=0;x<3;++x)
    for(int y=0;y<3;++y)
       if(board[x][y]!=' ')
	 ++e;
  if(e>=8)
    return 1;
  for (int i=0; i<3;i++)
	 if (board [i][0] == board [i] [1] && board [i][0] ==      board [i] [2] && board [i] [0] == ch)return 0;

for (i=0; i<3;i++)
	if (board [0][i] == board [1] [i] && board [0][i] == board [2] [i] && board [0] [i] == ch) return 0;


 if (board [0][0] == board [1] [1] && board [1][1] == board [2] [2] && board [1] [1] == ch)return 0;
 if (board [0][2] == board [1] [1] && board [1][1] == board [2] [0] && board [1] [1] == ch)return 0;
}
void func()
{
  for(int i=0;i<3;++i)
    for(int j=0;j<3;++j)
      {
	if(board[i][j]==' ')
	{
	  board[i][j]='O';goto d;
	}
      }
  d:
}
struct score
{
  int x,y,sco;
}z[7];
int best=0,value=0;
int computer(int player,int &x,int &y)\\problem
{
   if(player==1)
     best=-1000;
   if(player==-1)
     best=1000;
  if(check('X')==0) return -10;
  if(check('O')==0) return 10;
  if(check('X')==1&&check('O')==1) return 0;
  for(int i=0;i<3;++i)
    {
      for(int j=0;j<3;++j)
	{
	  if(board[i][j]==' ')
	    {
	       board[i][j]='O';
	       value=computer(-player,i,j);
	       board[i][j]=' ';
	    }
	  if(player==-1&&value<best)
	    best=value;
	  else if(player==1&&value>best)
	  {
	    best=value;
	    x=i;y=j;
	  }
       }
    }
  return best;
}
void main()
{
  randomize();
  int gd=DETECT,gm,player;
  initgraph(&gd,&gm,"");
//  anim();
  int a,b;
  startboard();
  for(int i=0;i<5;++i)
    {
      draw_board();
      user();
      a=b=0;
      computer(1,a,b);
      board[a][b]='O';
      if(check('X')==0)
	{
	  cout<<"\n\n\nX won";break;
	}
      if(check('O')==0)
	{
	  cout<<"\n\n\nO won";break;
	}

      closegraph();
      initgraph(&gd,&gm,"");

    }
  getch();
}
Last edited on
Edit your post and use code tags for your code - http://www.cplusplus.com/articles/jEywvCM9/
done
Please someone help.I am hoping to include this in my project in which submission date is Wednesday.I have googled for weeks still i can't implement this.
What is actually the problem ?
I don't know much about game theory, so I can't help you with your algorithm. However, your check() function doesn't return a value if all of the "if" statements fail.
closed account (48T7M4Gy)
Hi there JO31
1. First step is for you to understand and describe to us what the minimax algorithm is. Just a few short sentences will do.
2. Then you write up a plan - pseudocode is not a bad way of doing that.
3. Then you present it here with another post - you can even highlight the troublespot areas in that.
4. You can also pinpoint the corresponding areas in your code above if you like so we can get straight onto the problem for you instead of wading through piles of superfluous ...

Best wishes
Cheers :)
Ok kemort i will try to do that
closed account (48T7M4Gy)
Good man/woman. Winners are grinners!
Cheers
Last edited on
I think i implemented it.But it is not blocking all my moves.
closed account (48T7M4Gy)
So where in the problem? Where is the blocking code, and don't forget each side only has one move so at any particular turn, not necessarily every opponents move can be blocked of course.
Topic archived. No new replies allowed.