Computer Science Canada

Using the mouse

Author:  TheFerret [ Sun Mar 14, 2004 1:47 pm ]
Post subject:  Using the mouse

I am making a chess program and was going to use the keyboard to move the pieces but I think it is too complicated and I am going to use the mouse but I am not sure how to do it, I want the person to click the piece and then click on a open square and move the guy there but I do not know how to get the second click to work...

Author:  jonos [ Sun Mar 14, 2004 1:52 pm ]
Post subject: 

well you put your mousewhere(x, y, button) in a loop.

then when button = 1 (the user presses a mouse button), check if they are in a square where there is a chess peice.

if that is true, then make anotehr loop in the if statement you made above, and use mousewhere, and keep checking if the user has pressed a button, then when a button is pressed, check with an if statement it is in an unoccupied square and if it is a legal move, and if it is, move the chess peice and exit the loop.

it may sound complicated, but once you get everything together it will make sense. im sure there's an easier way but thats what i would do.

Author:  Cervantes [ Sun Mar 14, 2004 1:54 pm ]
Post subject: 

if youre using mousewhere it should work fine. However, if you are using mousewhere in combination with boolean variables (such as, if btn = 1 then button_pressed := true) then you may have forgot to set it back to false.

Anyways, post the code or just the necessary piece of it and we'll take a look Smile

BTW, I think using the arrow keys would be easier. Well, maybe not for knights Confused

Author:  jonos [ Sun Mar 14, 2004 1:57 pm ]
Post subject: 

you could also make it easier and do coordinates like the setting in chessmaster, but thats not as fun. it would be really easy though...

Author:  Cervantes [ Sun Mar 14, 2004 2:03 pm ]
Post subject: 

Sometimes the coolest programs are the simplest ones (Mostly here I'm referring to games, such as Space Invaders, pong, connect 4). However, chess I think is kinda the opposite. Since chess is a simple game to begin with and entering coords is no fun, you gotta make it look nice. It would be very cool if you could make it like this:

you select a unit by holding the mousebutton down, while the mouse is on him. Then you move the mouse around and when its a valid square for that unit to move to, it highlights the squares that that unit would take. It would be even cooler if you could animate the unit while he moves along that path.

But that's all for later, if you even want to do that. First do the basics and get the game working.

Author:  TheFerret [ Sun Mar 14, 2004 2:12 pm ]
Post subject: 

I made everything into a zip becasue there is like 12 .bmps... I have tried co-ods but it would get very confusing coding it...

Author:  Cervantes [ Sun Mar 14, 2004 2:24 pm ]
Post subject: 

having a ton of if statements like that for moving the pieces is very unefficient.
You should have a 2D array to store the positions of all the pieces. When the user inputs a coordinate (A2, for example) it checks that there is a piece at A2 and that it belongs to the correct player. Then you enter another coord and it checks if that piece can actually move there.

2D arrays... It's the way to go with these kind of board games.

Author:  TheFerret [ Sun Mar 14, 2004 2:31 pm ]
Post subject: 

And how would I do the 2D array...

Author:  Tony [ Sun Mar 14, 2004 2:33 pm ]
Post subject: 

you would go to [Turing Tutorials] and look up a tutorial or two

Author:  TheFerret [ Sun Mar 14, 2004 2:38 pm ]
Post subject: 

I mean how would I get to move the pieces of it and tell if a piece is in a square...

Author:  Paul [ Sun Mar 14, 2004 3:12 pm ]
Post subject: 

Fill the 2 D array with the pieces, like if at the start, the variable piece(1,1) would be filled with a black/white rook.

Author:  TheFerret [ Sun Mar 14, 2004 3:31 pm ]
Post subject: 

So I would have something like this
var board:array 1..8,1..8 of int

%and so on
But how would you move the pieces...

Author:  Tony [ Sun Mar 14, 2004 3:35 pm ]
Post subject: 

if pawn move from (4,2) to (4,4) then

board(4,2):="" %nothing in this spot

it would be easier if you write a function to just swap the two values. Just watch out for the case where you take out the opponent's piece, it has to disappear first

Author:  TheFerret [ Sun Mar 14, 2004 6:23 pm ]
Post subject: 

When I try to run
if move =1213 then

    board (1, 2) := board (1, 3)
    board (1, 2) := "" %nothing in this spot
it says Assigned value is the wrong type and highlights the ""...

Author:  Tony [ Sun Mar 14, 2004 6:59 pm ]
Post subject: 

maybe you should work on something simpler Laughing

Author:  Cervantes [ Sun Mar 14, 2004 7:11 pm ]
Post subject: 

ooh tony you can't give up now!

Do you have your 2D array as a string? Cuz that's what it should be.
Moving the pieces is the easies part. I think the harder part will come when your checking to see if a certain move is valid (ie. if the unit goes through another unit.)

Author:  jonos [ Sun Mar 14, 2004 10:27 pm ]
Post subject: 

not really, you just check:

if user dropped night on (2, 2), and it was originally on (1, 1), then the move is not valid, but if the user dropped the kngight on (2,3) and it was originally on (1,1), then its valid (i think thats right).

you just have to keep the original placement in an array or variable, then check it against the new one. thats just for a knight though;, i don't know about the other ones.

Author:  TheFerret [ Sun Mar 14, 2004 10:50 pm ]
Post subject: 

I had my array as an int instead of a string...

maybe you should work on something simpler
Thanks for the compliment... Laughing

Author:  Tony [ Sun Mar 14, 2004 11:00 pm ]
Post subject: 

heh, didn't mean to discourage you, but you did just plug in my pseudo-code into your program and wondered why variables types were off

Author:  TheFerret [ Sun Mar 14, 2004 11:05 pm ]
Post subject: 

The reason why I don't know this is becuase I haven't did it before and I want this to be a learning experiece for me to make me a better programmer... Crying or Very sad

Author:  Tony [ Sun Mar 14, 2004 11:08 pm ]
Post subject: 

chess might be a bit of an overwhealming learning experience Laughing

but I'll try to help Smile

Author:  TheFerret [ Sun Mar 14, 2004 11:14 pm ]
Post subject: 

I meant for learning arrays and stuff like that procedures and mouse...

Author:  the_short1 [ Mon Mar 15, 2004 1:29 pm ]
Post subject: 

i was thinking of makign chess... i though it would be ez... then i though about the movement.. and i said.. FUK dis... after i thougho of 3000 lines of code... not worth it... i want a simple program... less then 1500 lines

good abition ferret.... and if you read all the turotials on this site... you will be a way better programmer and be ahead of ur class in no time FLAT... then u can ask u teacher complicated stuff.. HEHE Wink :Very Happy

Author:  Tony [ Mon Mar 15, 2004 1:33 pm ]
Post subject: 

the_short1 wrote:
then u can ask u teacher complicated stuff.. HEHE Wink :Very Happy

Laughing generally speaking, once you get pass sorting arrays - your teacher is no help Laughing

Author:  the_short1 [ Mon Mar 15, 2004 1:50 pm ]
Post subject: 

good point... unless u compsci teacher ROCKS... but yea..

Author:  Cervantes [ Mon Mar 15, 2004 5:28 pm ]
Post subject: 

1-liner.. didn't answer a direct question... spam?

Anyways, Jonos -> that bit of code that you posted a ways back is all fine and dandy (yes, I know, I said dandy... Rolling Eyes) but its not efficient.

your code:

if board (1,1) = "whiteknight" then
if coord_input = "2C"  then
board (1,1) = " "
board (2,3) = "whiteknight"
end if
end if

Sure, it works. But it needs to be more generalized. That only works with the white knight in 1,1 going to 2,3.

I guess we'd need to make a function for each type of piece to check if it is a valid move.

Anyways, Confused what part of the prog are you on Ferret? Have you finished the basic movement and coordinate input?

Author:  TheFerret [ Mon Mar 15, 2004 6:04 pm ]
Post subject: 

Well I can get the pawns to move up one sqaure and I haven't tried the rest of the pieces....

P.S. How do you set the title for the top of the run window, I know that it has to do with setscreen and title in the brackets but I don't know the rest of the code...

Author:  the_short1 [ Mon Mar 15, 2004 6:21 pm ]
Post subject: 

setscreen ("graphics:600;400,title: My Chess Program")
View.Set ("graphics:600;400,title: My Chess Program")

and top get rid of that button bar

setscreen ("graphics:600;400,title: My Chess Program,nobuttonbar")
View.Set ("graphics:600;400,title: My Chess Program,nobuttonbar")

and to set the possition on where the window is..

setscreen ("graphics:600;400,title: My Chess Program,nobuttonbar,position:top;left")
View.Set ("graphics:600;400,title: My Chess Program,nobuttonbar,position:top;left")

although if you type setscreen or View.Set and then select it and press f9 it would have told u this... maybe look in the reference...that is what it is there for

Author:  Cervantes [ Mon Mar 15, 2004 6:44 pm ]
Post subject: 

Do you have the pawns moving like they were in the first code you posted where it used a bunch of if..elsif..end if statements to determine what was moved? Or have you got a better way of doing it?

Do you know how to use functions?

Author:  TheFerret [ Mon Mar 15, 2004 6:55 pm ]
Post subject: 

No, I do not know functions really well and it does not tell you how to make title in reference, I still have the same way as first post...

Author:  Cervantes [ Mon Mar 15, 2004 7:33 pm ]
Post subject: 

hmmm.... maybe you SHOULD work on something easier. Thinking
Now that I think more about it, this program is way more complicated than I first imagined.

*a long time later*

Ugh!! Every way I try to do it results in a large amount of if statements (not 1000+, but still Sad )

Here's what I've come up with to move a white pawn

I realize that the functions don't work because the board (row, col) actually equals something like "whtque", not 2 separate parts, "wht" and "que". You can just add in some string manipulation to get it to work Smile

function checkcolour (row, col)
    if board (row, col) = "wht" then
        result white
    elsif board (row, col) = "blk" then
        result black
    end if
end checkpiece
function checkpiece (row, col)
    if board (row, col) = "que" then
        result queen
    elsif board (row, col) = "pawn" then
        result pawn
    %elsif ............
        %result .....
    end if
end checkpiece
    get row1
    get col1
    get row2
    get col2
    if checkcolour (row1, col1) = white then
        if checkpiece (row1, col1) = pawn then
            if board (row2, col2) = " " then
                board (row2, col2) = board (row1, col1)
                board (row1, col1) = " "
            end if
        end if
    end if

That's mearly for a white pawn, without any detection of collisions with other pieces or validation that the move is legal. As you can see, its going to be a rough ride Neutral

Author:  the_short1 [ Mon Mar 15, 2004 8:02 pm ]
Post subject: 

i silently figured that out to (insert Large Number Here ) of code.. and that is why i didn;t make a chess/// i heard that someone acuallly made one tho//./ maybe thats just a rumur??? is it tru??? and where is it??

Author:  Cervantes [ Mon Mar 15, 2004 8:13 pm ]
Post subject: 

*Imitates Captain Jack Sparrow*
That's interesting...... That's very interesting...
I just did a search for "chess" using the "search" command and it returned a topic made not 3 hours ago. It can be found here.

Author:  the_short1 [ Mon Mar 15, 2004 8:44 pm ]
Post subject: 

i am talking about a n older one made a while back... wow... zylum just BAM made a wicked chess game... already works and lookz GREAT

Author:  AsianSensation [ Mon Mar 15, 2004 8:49 pm ]
Post subject: 

bugzpodder made one, I think you could get it from his website, it was a 2 player chess game, I'm pretty sure he didn't have AI with it.