473,606 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tree generation & minimax

4 New Member
I have been trying to learn how to do a c++ tic tac toe game, with first implementing the game tree in a function and then having the AI in another function which uses minimax algorithm. I came across the following code in my search for sources that would help me get a better understanding:

Expand|Select|Wrap|Line Numbers
  1.  
  2. /** evaluate board state from player's point of view */
  3.   int evaluate(Node node, int player) { /* using NEGMAX version of MINIMAX */
  4.      int value = 0;
  5.      if (wonFor(node.state, player)) {
  6.         value = 1; }
  7.      else if (wonFor(node.state, -player)) {
  8.         value = -1; }
  9.      else {
  10.         Vector successors = space.getSuccessors(node, -player);
  11.         Vector evaluations = new Vector();
  12.         for (int i = 0; i < successors.size(); i++) {
  13.            Node successor = (Node)successors.get(i);
  14.            successor.evaluation = evaluate(successor, -player);
  15.            if (successor.evaluation > value) value = successor.evaluation; }
  16.         value = -value; }
  17.      return(value);
  18.   }
  19.  
  20. Tree generation
  21.  
  22.   /** Generate tree of board states with evaluations appended */
  23.   Vector getTree(Node node, int player) {
  24.      Vector tree = new Vector();
  25.      tree.add(evaluate(node, player) + " for " + name(player) + " " + node);
  26.      if (winnerOf(node.state) == 0) {
  27.         Vector children = space.getSuccessors(node, -player);
  28.         for (int i = 0; i < children.size(); i++) {
  29.            tree.add(getTree((Node)children.elementAt(i), -player)); } }
  30.      return(tree);
  31.   }
  32.  
However though it may be close to what I am sort of looking for, I'm not very good at the java language and was wondering if someone could help me in relating it into c++?

-void ttt_gametree_ge nerate(ttt<Item >*, bool): generates a full game tree and the second argument bool indicates if it is the user's turn (true) or the agent's turn (false) // similar to the tree generation maybe?
-int ttt_bestmove(co nst ttt<Item>*): returns the best move of the agent, maybe what I'm looking for in the firs java function?

I'm trying to learn and having examples would help alot. I'm generating it with linked list over an array just for something different.
Thanks.
May 25 '06 #1
0 6344

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2199
by: qwweeeit | last post by:
Hi all, I am developing in Python (as a GUI I choosed Qt). To increase my expertise, besides reading manuals & tutorials, I am studying a big program developed in the language of my choice, and related with my project (to develop a card game). For that reason I choosed PySol (also if the GUI part uses Tkinter and not Qt). PySol is made up from almost 100 python modules and for that reason you need a mean to organize that. I developed...
1
8110
by: Adalbert | last post by:
First, I'm sorry for my english. Second, I've a little question: is some tree stucture like collections ArrayList or Queue to hold tree of same objects in .NET? Maybe I can use TreeView to that. I must hold tree of Halma's game (like checkers), which I generate with minimax algorithm. Hmm, is it good in english? :-) I hope :-) Thanks for all answers.
5
2224
by: Ruthless | last post by:
hello. All XML and XSLT are processed by preprocessor as a trees. How can i simply display my XML as some kind of tree. given xml: <struct> <node level="1" no="1">
19
6761
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
8
3653
by: marar.harish | last post by:
I need the following programs: Q. Write a c program that will allow you to enter and maintain a computerized version of your family tree.Begin by entering the number of generation.Then enter the names and natinalties in a hierarchy fashion, begiing with our own name and nationality.Include capabilities for modifying the tree and for adding new names to the tree.Also, include a provision for displaying the entire tree automatically after...
9
7820
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every chess position has around 30 moves, it would mean every node of the tree would have 30 branches (on average), which in turn themselves would average about 30 branches each. I can think of a variety of ways of implementing this, including a series...
3
2546
by: Thorsten Kampe | last post by:
Hi, This is a fairly general question: is there some kind of module or framework that allows building a tree like structure from certain kind of data? To be specific: I have a program that dumps the content of a LDAP directory including all properties and values and groups the result from the LDAP search by objClass.
1
2125
by: sourab123 | last post by:
hello i got frustrated after making several tries to make the minimax algorithm work in my console tic tac toe. the problem is the algorithm doesn't play the game as it should.The thing is it just tries to play the move that it 'sees' to be the best for itself without caring that its opponet is building an easy three to win the game. For ex. it playing as second player as ist player row=0;col=0; minimax player row=2;col=1; ist player...
1
2429
by: j_depp_99 | last post by:
Hi I wrote a program for a two player game that involves the difference between 2 given numbers. The players take turns entering the differences between two numbers that start as random numbers and then the board includes all differences entered so far. I found the possible solutions by implementing euclids algorithm. The game sort of works but I now need to implement it using minimax with alpha beta pruning. I dont understand how to...
0
8031
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8107
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8315
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6792
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3945
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.