minimax algorithm c# Minimax algorithm – CSE – IIT Kanpur

minimax algorithm c# Minimax algorithm – CSE – IIT Kanpur

FAQ
• Search • Login

View unanswered posts | View active topics It is currently Thu Nov 08, 2018 2:51 am

Board index » ROBOTC Applications » ROBOTC for LEGO MINDSTORMS

All times are UTC – 5 hours [ DST ]

Implement Minimax algorithm in ROBOTC

Moderators: mightor , starwarslegokid , miki


Reply to topic Page 1 of 1 [ 5 posts ] 
Print view Previous topic | Next topic  
Implement Minimax algorithm in ROBOTC 
AuthorMessage
ArmanUV
Rookie

Joined: Sat Nov 12, 2011 7:14 pm
Posts: 2

Post  Implement Minimax algorithm in ROBOTC
I am buiding a Tic Tac Toe solving robot for my school project. For practise, I wrote a Tic Tac Toe game using the minimax algorithm which worked very well. When I wanted to port my code to NXT, I found out that ROBOTC or any other C NXT compiler support recursive functions. How can change the following code so that it does not rely on recursion?

Code:
int miniMax (char board[BOARD_DIM][BOARD_DIM], _Bool minNode, int *xBest, int *yBest)
{
   int possibleMoves[NSQUARES][2];
   int nPossibleMoves = generateMoves(board, possibleMoves);
   char boardChild [BOARD_DIM][BOARD_DIM];
   int ind, x_ind, y_ind;
   int minScore, maxScore;
   if (gameOver(board))
      return evaluateState(board);
   else if (minNode)
   
      minScore = +INFINITY;
      for (ind = 0 ; ind < nPossibleMoves; ind++)
      
         duplicateBoard(board, boardChild);
         x_ind = possibleMoves[ind][0];
         y_ind = possibleMoves[ind][1];
         updateboard(boardChild, x_ind, y_ind, cPlayer);
         int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
         if (minScore > score)
            minScore = score;
      
      return minScore;
   
   else if (!minNode)
   
      maxScore = -INFINITY;
      for (ind = 0 ; ind < nPossibleMoves; ind++)
      
         duplicateBoard(board, boardChild);
         x_ind = possibleMoves[ind][0];
         y_ind = possibleMoves[ind][1];
         updateboard(boardChild, x_ind, y_ind, cComputer);
         int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
         if (maxScore < score)
         
            maxScore = score;
            *xBest = x_ind;
            *yBest = y_ind;
         
      
      return maxScore;
   

Code:
int generateMoves (char board[BOARD_DIM][BOARD_DIM], int possibleMoves[NSQUARES][2])

   int x_ind, y_ind, counter = 0;
   for(x_ind = 0 ; x_ind < BOARD_DIM ; x_ind++)
      for(y_ind = 0 ; y_ind < BOARD_DIM ; y_ind++)
         if(board[x_ind][y_ind] == EMPTY)
         
            possibleMoves[counter][0] = x_ind;
            possibleMoves[counter][1] = y_ind;
            counter++;
         
   return counter;

About the code :
evaluatestate() returns +1 if computer has won, -1 if player has won, and 0 if it is a draw.
gameover() returns 1 if game is over (somebody has won or it is a draw)
+INFINITY is +1 and -INFINITY is -1.
BOARD_DIM = 3 , NSQUARES = 9
cComputer = ‘O’ , cPlayer= ‘X’

Thanks :) !


Sat Nov 12, 2011 7:17 pm

Profile

ArmanUV
Rookie

Joined: Sat Nov 12, 2011 7:14 pm
Posts: 2

Post  Re: Implement Minimax algorithm in ROBOTC
In case somebody is actually interested in knowing the answer, using a "ghost" function like below worked for me:

Code:
char ghost(sBoard &board, bool minNode, char &xBest, char &yBest)

   return miniMax(board, minNode, xBest, yBest);

To developers: Any updates on support for recursion?


Tue Nov 15, 2011 3:10 am

Profile

mightor
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands

Post  Re: Implement Minimax algorithm in ROBOTC
The FW would need a complete internal rewrite to allow recursion so I am not so sure it will happen any time soon. I wouldn’t hold my breath :)

– Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I’d Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [ Project Page ]


Tue Nov 15, 2011 3:17 am

Profile WWW

amjadkhan0988
Rookie

Joined: Tue Aug 07, 2012 5:36 am
Posts: 1

Post  Re: Implement Minimax algorithm in ROBOTC
mightor wrote:
The FW would need a complete internal rewrite to allow recursion so I am not so sure it will happen any time soon. I wouldn’t hold my breath :)
– Xander

but it should need some more proper solution dud.. :programmer:

_________________
minimax solution


Tue Aug 07, 2012 5:37 am

Profile

JohnWatson
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 722

Post  Re: Implement Minimax algorithm in ROBOTC
This thread is a bit old, but ironically recursion and pointers are two major things we are working on for the next big update. Keep your eyes peeled, we plan to have it out sometime this fall.
_________________
Check out our Blog ! And our Facebook page !
Need help? Take a look at our updated help documentation and the ROBOTC Forums.


Tue Aug 07, 2012 9:00 am

Profile

Reply to topic   Page 1 of 1 [ 5 posts ] 

Board index » ROBOTC Applications » ROBOTC for LEGO MINDSTORMS

All times are UTC – 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum




Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.

Designed by ST Software for PTF .

Snipd
Handy snippets of code

  • About

« Custom fonts with the Google Fonts API
Test for a valid e-mail address and MX records in PHP »

Minimax algorithm with alpha-beta pruning in C#

By Ferhat | Published: July 28, 2011

Minimax is an algorithm where two players (min and max) play against each other for minimizing loss and maximizing gain. The following picture illustrates minimax with a game of tic-tac-toe.

Minimax with a game of tic-tac-toe

The algorithm uses a tree for the moves and scores of each player. In some cases, it isA�unnecessary for the algorithm to check certain subtrees. For example, if a certain player has already gained the best score in the subtree. A technique called alpha-beta pruning canadian select pharmacy reviews . can be applied to let the algorithm ‘prune’ certain subtrees.

The following snippet contains the minimax algorithm with alpha-beta pruning.

 public class AlphaBeta bool MaxPlayer = true; public AlphaBeta() public int Iterate(Node node, int depth, int alpha, int beta, bool Player) if (depth == 0 

The Node class contains methods which can vary per game.

 public class Node public Node() public List<Node> Children(bool Player) List<Node> children = new List<Node>(); // Create your subtree here and return the results return children; public bool IsTerminal(bool Player) terminalNode = false; // Game over? return terminalNode; public int GetTotalScore(bool Player) int totalScore = 0; // This method is a heuristic evaluation function to evaluate // the current situation of the player // It depends on the game. For example chess, tic-tac-to or other games suitable // for the minimax algorithm can have different evaluation functions. return totalScore; 

 

This entry was posted in C# and tagged ai , algorithm , alpha beta , artificial intelligence , beta lt , c# , c-sharp , chess , development , gaming , Minimax , node , player , programming , pruning , return beta , tac toe , tic-tac-toe . Bookmark the permalink . Post a comment or leave a trackback: Trackback URL .

« Custom fonts with the Google Fonts API
Test for a valid e-mail address and MX records in PHP »

One Trackback

  1. By How to implement c# tree using List<T> (for Pentago AI ) | BlogoSfera on June 23, 2013 at 10:03 am

    […] trying to implement Pentago AI and found the code snippet below in the : “ http://snipd.net/minimax-algorithm-with-alpha-beta-pruning-in-c#8221 ; about alpha beta pruning and evaluation function . As you see in code snippet a subtree can […]

Post a Comment Cancel reply

  • Advertisement

  • Categories

    • ActionScript
    • Assembler
    • Bash
    • C
    • C#
    • C++
    • F#
    • Haskell
    • Java
    • JavaScript
    • Lua
    • Objective-C
    • Perl
    • PHP
    • Prolog
    • Python
    • Ruby
    • Uncategorized
  • Tags

    ActionScript
    algorithm
    Anacin
    App
    Assembler
    bash
    Buy
    c#
    c++
    c-sharp
    canabis
    Cheap
    class
    cocoa framework
    CSS
    Cymbalta
    Depression
    development
    essays
    gaming
    graphics
    haskell
    html
    instance
    ios
    iPhone
    java
    javascript
    jquery
    Lady era
    math
    objective-c
    Online
    order
    Paper
    phone
    php
    Pills
    programming
    Purchase
    python
    seeds
    snippet
    Spy
    writing
  • Recent Comments

    • Vahram on Huffman coding in C#
    • Simon on Quicksort algorithm in C#
    • Anas Jabar on Huffman coding in C#
    • Quicksort algorithm in C# | Marin Takanovs blog on Quicksort algorithm in C#
    • Steve on Parsing XML in C#

© 2018 Turing Technology Services | Got snippets? | Who is SnipBot?

About the Author: admin