Question

Working with GLUT to create a connect 4 program. I'm struggling to figure out how to...

Working with GLUT to create a connect 4 program. I'm struggling to figure out how to attach a circle token ( of either black or red depending on the player) to be attached to the mouse . So, the circle always moves with the mouse, and when the player clicks wherever, as long as its a good move, changes the other players color. I have no idea how to do this. I have a cursor function:

void cursor(int x, int y) {} and a drag function: void drag(int x, int y){} and a function for clicking, that I have already implemented. Any ideas?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#This I have implemented in python as you have not mentioned the language that you are using

def draw(a=[]):
def tc(a):
if a==0: return " "
if a==1: return "X"
if a==2: return "Y"
print "\n\n --------------------------- "
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[5][0]),tc(a[5][1]),tc(a[5][2]),tc(a[5][3]),tc(a[5][4]),tc(a[5][5]),tc(a[5][6]))
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[4][0]),tc(a[4][1]),tc(a[4][2]),tc(a[4][3]),tc(a[4][4]),tc(a[4][5]),tc(a[4][6]))
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[3][0]),tc(a[3][1]),tc(a[3][2]),tc(a[3][3]),tc(a[3][4]),tc(a[3][5]),tc(a[3][6]))
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[2][0]),tc(a[2][1]),tc(a[2][2]),tc(a[2][3]),tc(a[2][4]),tc(a[2][5]),tc(a[2][6]))
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[1][0]),tc(a[1][1]),tc(a[1][2]),tc(a[1][3]),tc(a[1][4]),tc(a[1][5]),tc(a[1][6]))
print " [%s] [%s] [%s] [%s] [%s] [%s] [%s] "%(tc(a[0][0]),tc(a[0][1]),tc(a[0][2]),tc(a[0][3]),tc(a[0][4]),tc(a[0][5]),tc(a[0][6]))
print " --------------------------- "

print " 1 2 3 4 5 6 7 \n\n"

#valid moves
order=[3,2,4,1,5,0,6]
def validMoves(intable):
global order
moves=[]
for col in order:
for row in range(6):
if intable[row][col]==0:
moves.append([row,col])
break
return moves
#moves in slot x acording to valid moves function
def move(intable,x,who):
val=validMoves(intable)
intable[val[x][0]][val[x][1]]=who
#Alpha Beta Pruning Search Algorithm
def alphabetaPruning(intable, depth):
def ab(intable, depth, alpha, beta):
values=[]; v=-10000000
for a,s in validMoves(intable):
intable[a][s]=1
v=max(v, abmin(intable, depth-1, alpha, beta))
values.append(v)
intable[a][s]=0
largest=max(values)
dex=values.index(largest)
return [dex, largest]
def abmax(intable, depth, alpha, beta):
moves=validMoves(intable)
if(depth==0 or not moves):
return eval(intable)
v=-10000000
for a,s in moves:
intable[a][s]=1
v=max(v, abmin(intable, depth-1, alpha, beta))
intable[a][s]=0
if v >= beta: return v
alpha=max(alpha, v)
return v
def abmin(intable, depth, alpha, beta):
moves=validMoves(intable)
if(depth==0 or not moves):
return eval(intable)
v=+10000000
for a,s in moves:
intable[a][s]=2
v=min(v, abmax(intable, depth-1, alpha, beta))
intable[a][s]=0
if v <= alpha: return v
beta=min(beta, v)
return v
return ab(intable, depth, -10000000, +10000000)
#returns the minutes*60 + seconds in the actial time
def time(): return ((gmtime()[4])*60)+gmtime()[5]
#Iterative Deepening Search Algorithm
def iterDeepening(intable):
global order
#order=[3,2,4,1,5,0,6]
timeout=time()+19
depth=1
res=alphabetaPruning(intable, depth)
while True:
tStart=time()
if abs(res[1])>5000: #terminal node
print "Nearly done!"
return res[0]
tmp=res[0]
#chaning the order in considering moves
while tmp!=0:
order[tmp-1],order[tmp]=order[tmp],order[tmp-1]
tmp-=1
depth+=1
res=alphabetaPruning(intable, depth)
tEnd=time()
runTime=tEnd-tStart
if timeout < tEnd+(4 * runTime) or depth > 42:
print "DEPTH", depth
return res[0]
Add a comment
Know the answer?
Add Answer to:
Working with GLUT to create a connect 4 program. I'm struggling to figure out how to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Connect 4 is a 2 player game where each player has a set of colored tokens...

    Connect 4 is a 2 player game where each player has a set of colored tokens (red or yellow). Players take turns during which they place a single token into one of the columns of an n by m grid (where n is the number of rows and m is the number of columns. They place their token into a slot at the top of the column and it falls into the lowest unoccupied slot in that column. A player...

  • Have been working on this code for awhile and cant get it to work with Glut....

    Have been working on this code for awhile and cant get it to work with Glut. Wondering if there is anything I can fix #include <iostream> #include "graph1.h" using namespace std; //Add Function Prototypes Here int getNoPoints(); void getPoints(int x[], int y[], int no_points); void drawPolyLine(int x[], int y[], int no_points); int main() {    //Variable Declaration/Initialization    int no_points = 0;    const int MAX_POINTS = 10;    int x[MAX_POINTS];    int y[MAX_POINTS];    //Display Graphics Window    displayGraphics();...

  • This task is a program framework that you should complete. The program should allow the user...

    This task is a program framework that you should complete. The program should allow the user to move a circular figure with the mouse over a drawing area. The current position of the figure is displayed continuously: Given is the main program: import javafx.scene.Scene; import javafx.application.Application; import javafx.beans.value.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; public class Main extends Application { private DraggableCircle dc; private Text text; private void updateText() { text.setText("("+dc.getCenterX()+", "+dc.getCenterY()+")"); } @Override public void start(final Stage...

  • Hi! I am struggling trying to figure out how to do this. For this lab, you...

    Hi! I am struggling trying to figure out how to do this. For this lab, you must have two implementations of calculating the Fibonacci sequence. You are required to calculate it utilizing an array and then with a pointer. You must use the array provided and you must malloc space for your pointer. Your algorithms must be in-place, meaning they do not use a temporary variable to calculate the next sequence number. Lastly, you must insert a comment above each...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: ....

    C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: . Reads a text file provided along with this data and maintains it an object. Determines the number of characters and keeps in the object. Determines the number of words and retains the result in the object. Determines the number of paragraphs and keeps the result in the object. A possible class definition: class Textutil { string text = ** int words = @; int...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT