• Create Account

# Ars7c3

Member Since 22 May 2011
Offline Last Active Jul 05 2015 05:46 PM

### Help with MiniMax Algorithm for Tic Tac Toe

02 July 2015 - 11:09 AM

I am currently working on creating an AI player for tic tac toe. After researching, I discovered that the minimax algorithm would be perfect for the job.

I am pretty confident in my understanding of the algorithm and how it works, but coding it has proven a little bit of a challenge. I will admit, recursion is one of my weak areas . The following code is my AI class. It currently runs, but it makes poor decisions. Could someone please point out where I went wrong? Thank You!

```import tictactoe as tic   # interface to tictactoe game logic like check_victory

class AI:
def __init__(self, mark):
self.mark = mark

def minimax(self, state, player):
#end condition - final state
if tic.check_victory(state):
if player == self.mark:
return 1
else:
return -1
if tic.check_cat(state):
return 0

nextturn = tic.O if player == tic.X else tic.X
#generate possible moves
mvs = []
for i, mark in enumerate(state):
if mark == tic.EMPTY:
mvs.append(i)

#generate child states of parent state
scores = []
for mv in mvs:
leaf = state[:]
leaf[mv] = player
result = self.minimax(leaf, nextturn)
scores.append(result)

if player == self.mark:
maxelle = max(scores)
return mvs[scores.index(maxelle)]
else:
minele = min(scores)
return mvs[scores.index(minele)]

def make_move(self, board, player):
place = self.minimax(board, player)
return place

```

### problems with initializing my map

15 June 2013 - 11:04 AM

Hey guys,
so I am working on a simple level editor for a platform game I&#39;m making, and everything&#39;s been going smoothly up until now.
I decided I wanted to have a 3d vector to allocate tiles for my editor instead of a 3d pointer because they are very difficult to understand( for me anyway). The level allocates correctly ONLY if the width, height, and layers of the level are all the same value, and that is what is stumping me.
I would greatly appreciate it if someone could help me out.

```//My vector container
vector<vector<vector<Tile*> > >map;

//How I'm allocating the map... in the Level constructor
Level::Level(int tileWidth, string tilePath)
{
sheet.Init(tileWidth, tilePath);

for(int l = 0; l < layers; l++)
{
map.resize(layers);
cout << "Layer " << l << endl;

for(int i = 0; i < height; i++)
{
map[i].resize(height);
cout << endl<< "Row " << i << endl;
for(int t = 0; t < width; t++)
{
map[i][t].resize(width);
map[i][t].push_back(new Tile(t * sheet.GetTileSize(), i * sheet.GetTileSize(), false, sheet));
cout << "X " << t << endl;
}
}
}

}

//How i am saving the level -- using integers to represent tile types
void Level::Save()
{

cout << "NOPE" << endl;
ofstream file(fileName.c_str());
for(int x = 0; x < layers; x++)
{
file << endl << endl << "Layer: " << x + 1 << endl << endl;
for(int y = 0; y < height; y++)
{
file << endl;
for(int z = 0; z < width; z++)
{
file << map[x][y][z]->GetType();
}
}
}

}

```

### !HELP! SFML Window Init Problems

09 June 2013 - 04:43 PM

Hello,

I've had issues initializing sf::RenderWindow and cannot figure out why the program isn't working. I am getting an unhandled exception error.

The program crashes right when i call the create function for RenderWindow. The code is below.

```//This is the whole Engine.h file

#ifndef ENGINE
#define ENGINE
#include <string>
#include <vector>
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Debug.h"
using namespace std;

class State;
class Engine
{
public:
void Init(int Width, int Height,string caption);
void CleanUp();

void ChangeState(State *state);
void PushState(State *state);
void PopState();

void HandleEvents();
void Update();
void Render();
void Run();

bool Running();
void Quit();

sf::RenderWindow *Window();
private:
sf::RenderWindow *m_window;
vector<State*> m_states;
bool m_running;
int m_width, m_height;
};

#endif

//This is the Init function in the cpp file for Engine
void Engine::Init(int Width, int Height,string caption)
{
Debug::Write("Starting");

m_states.clear();
Debug::Write("states cleared");
m_width = Width;
m_height = Height;
Debug::Write("w/h init");
m_running = true;
Debug::Write("running = true");
m_window->Create(sf::VideoMode(Width,Height),caption);
Debug::Write("!Engine initialization complete...");
}
```

### Collision Detection HELP!

14 August 2012 - 05:35 PM

Okay, so i have been working on a pong game in order to test my very basic game engine, and everything that the engine is supposed to do it's doing. The problem is in the collision code, which is confusing considering I have used this very same code successfully in other games I have made. I honestly have no idea what the issue is, and I was hoping one of you guys could help me. I don't know if this would make a difference or not but I am using Dev C++.

Here is the collision code and how it is used (BTW I made sure the SDL_Rect coordinates are correct)

[source lang="cpp"]bool Collision(SDL_Rect A, SDL_Rect B){ //The sides of the rectangles int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; //Calculate the sides of rect A leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; //Calculate the sides of rect B leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; //If any of the sides from A are outside of B if( bottomA <= topB ) { return false; } else if( topA >= bottomB ) { return false; } else if( rightA <= leftB ) { return false; } else if( leftA >= rightB ) { return false; } //If none of the sides from A are outside B else return true;}[/source]

And here is how I call it in the main game loop:
[source lang="cpp"] if(Collision(paddle2.GetRect(),ball.GetRect())) { cout << "Hit" << endl; } [/source]

25 June 2012 - 08:18 AM

Okay so i have been working on a very simple game engine in order to ease my development on games. So far it has been going well, until I discovered a bug. I have a texture manager that can create or add textures to a vector that contains all of the texture objects. It works perfectly when I only add 1 texture to the game, but when i try to add multiple textures the game window pops up and disappears which leads me to believe something is either wrong in my Texture class or my TextureManager Draw function or it's AddTexture function. I have been stuck on this for days, and would really appreciate some help. I have attached the code so you could possibly take a peek.

PS. I am 14 so i might have made some stupid mistakes.

Code: (Just the Texture and TextureManager header/source files, everything else works fine)

[source lang="cpp"]namespace Smitty{ class cTexture { public: cTexture(); cTexture(string filename, string Name); ~cTexture(); int GetX(); int GetY(); int GetW(); int GetH(); void SetX(int x); void SetY(int y); string GetFilename(); void SetFilename(string name); string GetName(); void SetName(string Name); SDL_Surface* GetTexture(); SDL_Surface *texture; void DrawTexture(SDL_Surface *Surface, int x, int y); void DrawTextureSection(SDL_Surface *Surface, SDL_Rect *rect, int x, int y); private: int Width; int Height; int X,Y; string Filename; string name; };}[/source]

(This is just the Draw Texture function
)
void cTexture::DrawTexture(SDL_Surface *Surface, int x, int y)
{

SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = Width;
dest.h = Height;

SDL_BlitSurface(texture,NULL,Surface, &dest);

}
Alright so here is the TextureManager Class
[source lang="cpp"]namespace Smitty{class cTextureManager{ public: cTextureManager(); ~cTextureManager(); SDL_Surface *LoadTexture(string filename); void ReloadAllTextures(); void CreateTexture(string filename, string texName); //Creates a new texture and adds it to the Textures vector void AddTexture(cTexture &texture); void FreeTexture(string texName); void FreeAllTextures(); void DrawTexture(string texName, SDL_Surface *Destination, int x, int y); //Draws one texture void DrawTexture(string texName, SDL_Surface *Destination); void DrawAllTextures(SDL_Surface *Destination); //Draws all textures int GetTextureWidth(cTexture texName); int GetTextureHeight(cTexture texName); int GetTextureX(string texName); int GetTextureY(string texName); void SetTextureX(string texName, int value); void SetTextureY(string texName, int value); void GetSize(); protected: vector<cTexture> Textures; };[/source]
Now here are the problematic functions in the TextureManager class
[source lang="cpp"]SDL_Surface *cTextureManager::LoadTexture(string filename){ SDL_Surface *temp = NULL; SDL_Surface *optimized = NULL; temp = SDL_LoadBMP(filename.c_str()); if(temp != NULL) { optimized = SDL_DisplayFormat( temp ); SDL_FreeSurface( temp ); cout << "Optimized Image Created." << endl; } return optimized;} //Creates a new cTexture and adds it to the Textures vectorvoid cTextureManager::CreateTexture(string filename, string texName){ cTexture temp; temp.SetFilename(filename.c_str()); temp.SetName(texName); AddTexture(temp); cout << "Texture Added." << endl;}//Adds a already instiated cTexture to the Textures vectorvoid cTextureManager::AddTexture(cTexture &texture){ // CreateTexture(texture.GetFilename(), texture.GetName()); string filename; string name; filename = texture.GetFilename(); name = texture.GetName(); texture.texture = LoadTexture(filename.c_str()); texture.SetName(name.c_str()); Textures.push_back(texture); cout << "Texture Added." << endl;}[/source]
//Draws a texture at user specified coordinates
void cTextureManager::DrawTexture(string texName, SDL_Surface *Destination, int x, int y)
{
vector<cTexture>::iterator it;

for(it = Textures.begin(); it < Textures.end(); it++)
{
if(it->GetName() == texName)
it->DrawTexture(Destination,x,y);
}

}

//Draws texture at textures coordinates instead of user specified cords.
void cTextureManager::DrawTexture(string texName, SDL_Surface *Destination)
{
vector<cTexture>::iterator it;

for(it = Textures.begin(); it < Textures.end(); it++)
{
if(it->GetName() == texName)
it->DrawTexture(Destination,it->GetX(),it->GetY());
}
}

//Draws all textures at thier coordinates
void cTextureManager::DrawAllTextures(SDL_Surface *Destination)
{

vector<cTexture>::iterator it;

for(it = Textures.begin(); it < Textures.end(); it++)
{
it->DrawTexture(Destination,it->GetX(),it->GetY());
}
}[/source]

PARTNERS