Miscellaneous Examples from Class

(new stuff added at the beginning; scroll down for older stuff.)

nov 19

to find a blocking move for a gamestate

make sure you AVOID any move that lets the opponent win

in the gamestate

to tell if a move lets the opponent win in a gamestate

apply the move to the gamestate

find the best move for that gamestate //RECURSION

//this will be a move for the OPPONENT

apply that move to the gamestate

if the player who last played has won, return 1;

return 0;

//NOTE that this approach assumes that your move finder checks for

//winning moves and takes them!!

----------------------------------------------

to find best move for a gamestate:

generate all moves

use knockout plan to find legal move with highest score

in this gamestate

return that move

to find score for a move in a gamestate

apply the move to the gamestate //notes 7

return the score for the resulting gamestate

to find score for a gamestate

if the player who just played won, return 100

if the game is a draw, return 0; //no legal moves

find the best move for this gamestate //recursion!

return -1 * the score for this move in this gamestate

//because the score is for the opponent

another approach

struct evaluated_move

{

struct move m;

int value;

}

to find best move in a gamestate

find best evaluated_move in the gamestate

return the move part of the evaluated_move

to find best evaluated_move in a gamestate

generate all moves

if there are no legal moves

create a dummy move and set its value to 0 //draw

if there is a winning move

set its value to 100 and return it

use knockout plan to find move with highest value

in this situation

create evaluated_move with this move and value

return it

to find value of a move in a situation

apply move to the situation

find best evaluated_move in the new situation //recursion

return -1 * the value of this move

//because this is the opponent's move

oct 24

example code to troubleshoot

Sep 22

int x,z;

x=foo(3,&z);

//x should now be 27 [the cube of 3],z should be 9 [the square of 3]

//which, if any, of the following definitions of foo() will work?

int foo(int a,int* b) //1

{

*b=a*a;

printf("%d",a*a*a);

}

int foo(int a,int* b) //2

{

return a*a*a;

*b=a*a;

}

int foo(int a,int* b) //3

{

*b=a*a;

return int a*a*a;

}

int foo(int a,int* b) //4

{

*b=&b;

*b=a*a;

return a*a*a;

}

int foo(int a,int* b, int *c) //5

{

*b=a*a;

return a*a*a;

}

int foo(int *3,int* b) //6

{

*b=a*a;

return a*a*a;

}

int foo(int a,int &b) //7

{

*b=a*a;

return a*a*a;

}

int foo(int a) //8

{

return a*a;

return a*a*a;

}

int foo(int *a,int* b) //9

{

*b=a*a;

return a*a*a;

}

Sep 7 program for examining addresses of array elements

#include <stdio.h>

int main()

{

int i;

char eggplant[4];

eggplant[0]='a';

eggplant[1]='b';

eggplant[2]='c';

eggplant[3]='d';

printf("the value is %d\n",eggplant[3]);

printf("the address of eggplant is %d\n",(int)&eggplant);

for (i=0;i<25;i++)

eggplant[3]='x';

//eggplant[6]='q';

printf("and after the assignment\n");

for (i=0;i<25;i++)

while(1)

{

printf("Type a subscript: ");

scanf("%d",&i);

printf("the address of eggplant[i] is %d\n",(int)&eggplant[i]);

}

return 0;

}

Sep 3 programs for looking at structs and arrays in memory

#include <stdio.h>

int main()

{

int i;

int eggplant[4];

eggplant[0]=31;

eggplant[1]=32;

eggplant[2]=33;

eggplant[3]=34;

printf("the address of eggplant is %d\n",(int)&eggplant);

while(1)

{

for (i=0;i<25;i++)

}

return 0;

}

#include <stdio.h>

struct point

{

int x;

int y;

};

int main()

{

int i;

struct point p;

printf("the address of point is %d\n",(int)&p);

while(1)

{

for (i=0;i<25;i++)

}

return 0;

}

Sep 1: the program for looking at bits in memory

#include <stdio.h>

int main()

{

int i;

char okra='w';

int eggplant=257;

char broccoli='W';

printf("the address of eggplant is %d\n",(int)&eggplant);

printf("the address of broccoli is %d\n",(int)&broccoli);

while(1)

{