
-----------------------------------
TheFerret
Sun Mar 14, 2004 1:47 pm

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...

-----------------------------------
jonos
Sun Mar 14, 2004 1:52 pm


-----------------------------------
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.

-----------------------------------
Cervantes
Sun Mar 14, 2004 1:54 pm


-----------------------------------
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 :)

BTW, I think using the arrow keys would be easier.  Well, maybe not for knights :?

-----------------------------------
jonos
Sun Mar 14, 2004 1:57 pm


-----------------------------------
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...

-----------------------------------
Cervantes
Sun Mar 14, 2004 2:03 pm


-----------------------------------
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.

-----------------------------------
TheFerret
Sun Mar 14, 2004 2:12 pm


-----------------------------------
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...

-----------------------------------
Cervantes
Sun Mar 14, 2004 2:24 pm


-----------------------------------
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.

-----------------------------------
TheFerret
Sun Mar 14, 2004 2:31 pm


-----------------------------------
And how would I do the 2D array...

-----------------------------------
Tony
Sun Mar 14, 2004 2:33 pm


-----------------------------------
you would go to [url=http://www.compsci.ca/v2/viewforum.php?f=3][Turing Tutorials] and look up [url=http://www.compsci.ca/v2/viewtopic.php?t=366] a tutorial or [url=http://www.compsci.ca/v2/viewtopic.php?t=1117] two

-----------------------------------
TheFerret
Sun Mar 14, 2004 2:38 pm


-----------------------------------
I mean how would I get to move the pieces of it and tell if a piece is in a square...

-----------------------------------
Paul
Sun Mar 14, 2004 3:12 pm


-----------------------------------
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.

-----------------------------------
TheFerret
Sun Mar 14, 2004 3:31 pm


-----------------------------------
So I would have something like this var board:array 1..8,1..8 of int

board(1,1):=whtrook
board(1,2):=whtkni
board(1,3):=whtbis
board(1,4):=whtkin
%and so on But how would you move the pieces...

-----------------------------------
Tony
Sun Mar 14, 2004 3:35 pm


-----------------------------------
if pawn move from (4,2) to (4,4) then

board(4,4):=board(4,2)
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

-----------------------------------
TheFerret
Sun Mar 14, 2004 6:23 pm


-----------------------------------
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 ""...

-----------------------------------
Tony
Sun Mar 14, 2004 6:59 pm


-----------------------------------
:shock:
maybe you should work on something simpler :lol:

-----------------------------------
Cervantes
Sun Mar 14, 2004 7:11 pm


-----------------------------------
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.)

-----------------------------------
jonos
Sun Mar 14, 2004 10:27 pm


-----------------------------------
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.

-----------------------------------
TheFerret
Sun Mar 14, 2004 10:50 pm


-----------------------------------
I had my array as an int instead of a string...

maybe you should work on something simpler  Thanks for the compliment... :lol:

-----------------------------------
Tony
Sun Mar 14, 2004 11:00 pm


-----------------------------------
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

-----------------------------------
TheFerret
Sun Mar 14, 2004 11:05 pm


-----------------------------------
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...  :cry:

-----------------------------------
Tony
Sun Mar 14, 2004 11:08 pm


-----------------------------------
chess might be a bit of an overwhealming learning experience :lol:

but I'll try to help :)

-----------------------------------
TheFerret
Sun Mar 14, 2004 11:14 pm


-----------------------------------
I meant for learning arrays and stuff like that procedures and mouse...

-----------------------------------
the_short1
Mon Mar 15, 2004 1:29 pm


-----------------------------------
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 ;) ::D

-----------------------------------
Tony
Mon Mar 15, 2004 1:33 pm


-----------------------------------
then u can ask u teacher complicated stuff.. HEHE ;) ::D

 :lol: generally speaking, once you get pass sorting arrays - your teacher is no help :lol:

-----------------------------------
the_short1
Mon Mar 15, 2004 1:50 pm


-----------------------------------
good point... unless u compsci teacher ROCKS... but yea..

-----------------------------------
Cervantes
Mon Mar 15, 2004 5:28 pm


-----------------------------------
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... :roll:) 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, :? what part of the prog are you on Ferret?  Have you finished the basic movement and coordinate input?

-----------------------------------
TheFerret
Mon Mar 15, 2004 6:04 pm


-----------------------------------
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...

-----------------------------------
the_short1
Mon Mar 15, 2004 6:21 pm


-----------------------------------
setscreen ("graphics:600;400,title: My Chess Program")
or
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")
or
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")
or
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

-----------------------------------
Cervantes
Mon Mar 15, 2004 6:44 pm


-----------------------------------
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?

-----------------------------------
TheFerret
Mon Mar 15, 2004 6:55 pm


-----------------------------------
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...

-----------------------------------
Cervantes
Mon Mar 15, 2004 7:33 pm


-----------------------------------
hmmm.... maybe you SHOULD work on something easier.  :think:
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 :( )

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 :)

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 :|

-----------------------------------
the_short1
Mon Mar 15, 2004 8:02 pm


-----------------------------------
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??

-----------------------------------
Cervantes
Mon Mar 15, 2004 8:13 pm


-----------------------------------
*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 [url=http://www.compsci.ca/v2/viewtopic.php?t=4156&highlight=chess]here.

-----------------------------------
the_short1
Mon Mar 15, 2004 8:44 pm


-----------------------------------
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

-----------------------------------
AsianSensation
Mon Mar 15, 2004 8:49 pm


-----------------------------------
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.
