#ifndef ROBOT_H_INCLUDED
#define ROBOT_H_INCLUDED
#include "minimax.h"
constint min = -10000;
constint max = 10000;
int* robot(santorini game, int Depth, int player){
byte movelist[][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} };
int value, v;
int moves[6];
int movesToTake[6];
int who = 2-(game.numOfMove%2)*2; //player1's turn = 0, player2's turn = 2
if (2-game.numOfMove%2==player){
santorini botgame = game;
for(int a=0; a<=1; a++){
for (int i=0; i<=7; i++){
int x = botgame.piece[a+who][0];
int y = botgame.piece[a+who][1];
botgame.move(x, y, player);
if (botgame.Compare(botgame,game)){
moves[0]=x;
moves[1]=y;
santorini botgame2 = botgame;
for (int j=0; j<=7; j++){
x += movelist[j][0];
y += movelist[j][1];
botgame.move(x, y, player);
if (botgame.Compare(botgame,botgame2)){
moves[2]=x;
moves[3]=y;
santorini botgame3 = botgame;
for (int k=0; k<=7; k++){
x += movelist[k][0];
y += movelist[k][1];
botgame.move(x, y, player);
if (botgame.Compare(botgame,botgame3)){
moves[4]=x;
moves[5]=y;
v = minimax(botgame, Depth, min, max, player);
if (v>value){
value = v;
for (int z=0;z<=5;z++){
movesToTake[z]=moves[z];
}
}
}
}
}
}
}
}
}
return movesToTake;
}
}
#endif // ROBOT_H_INCLUDED
You should not have cpp code in headers. Put the class definition only in the header. The class functions definitions go in a header .cpp file with the same name as the class. Put the scope resolution before the function names. Example
Include the class header file in whichever .cpp file needs to use an object of that type.
If you are using an IDE, you should be able to get the class wizard to do the outline of these things for you (At least it does on mine)
In the file with main() in it - declare the functions before main, then put the definitions of them after main. This way we don't have to scroll down to see what main does.
That should help a great deal with the organisation of your files.
As far as trying to see where the problem is, I am not that keen to go through 500 lines of code to try and see a problem. Learn to use the debugger. If it doesn't compile, then post the compiler output in full.
Also in general, you might need to think about the design of your code. Having lots of nesting may mean there is a problem with that. And the use of functions. If there are more than 5 lines of code in a switch case - it might be worth putting that in a function - even if it is just for ease of reading. Same for loops and other control-flow statements. There is a bit of an unwritten rule that functions should not be longer than 80 lines and lines not more than 80 chars for readability.
Get back to us when you have made all these changes so we can see how you went. Hope all goes well.
Thank you soooo much for your suggestions, they are staight to the point.
Sorry for my messy code, you know...I am just a beginner, so I really need advices on how to structure my program.
BTW:
(1)In a game program lik this, we usually need to make a class that represent all gameplays, game board and pieces right, where should I instantiate that game class?
(2)In a game program like this, what class should I make? A class that represent all gameplays and any what else?javascript:editbox1.editSend()
(3)How to debug? (I know this question is too broad, but I really have no idea how to ask)
First, I edited my last post a little. Class member functions go in a .cpp file.
Your questions:
1. A Game object could be could be instantiated in any .cpp file that is separate to the class function definitions, so this probably means in the main() function for this project.
2. Class design is trickier than what a lot of people think - it really depends on exactly what you want to do. There are several relationships: "IS A" (Implies inheritance); "HAS A" (One class contains another object as a member variable of that type); "USES A" (An object of a class type is passed as an argument to a class function). You should do some research of your own, by Googling these topics - and have a good read. Generally, you should separate things into 'Objects' like they would be in the real world, then have a think about how they are going to interrelate, not just for the data they store, but also for the functions they have - "The Interface". This whole thing is a big topic - entire books have been written about it.
3. If you have an IDE, it should be easy. There should be a Debug Menu. You can select breakpoints where the code stops. Set a watch list so you can track the value of all the variables & see how they change as you step through the code 1 line at a time. You can then deduce where your logic is screwed up. However I think you need to restructure your code quite a bit first.
If you compile from the shell, then it is a bit trickier to learn a command line debugger like gdb, which works with g++. There is an article in the documentation section of this website, on the top left of this page. Also lots of reference info you should read.
I have been thinking about how I would do this myself, I would like to be able to do plugins, so that I can add new things without redoing a bunch of code, no RTTI or dynamic casting, no overloading of functions, and minimisation of coupling by using design patterns. I will be making extensive use of polymorphic virtual functions.
One observation: Game programming can get very complex quickly - if you are a brand new beginner, you might consider keeping everything very simple to start with. Get the simple thing working first, then add complexity gradually, making sure it all works for each new thing. My program design is only going to have Players, Enemies, Resources (such as ammo, medi packs etc), Wepons, Actions and Tiles, and it already has some advanced ideas (Well IMO anyway)