Making a chess multiplayer game - vb.net

I am trying to make a Chess multiplayer game in Visual Basic. Its a two player which will be played in a normal way and not even over LAN.
So far, I am done designing the board.
My logic is:
First, on clicking any square, the click event handler will check whether a piece resides on that square. If not, then it checks if a piece is to be moved to that square. If both are negative, then it does nothing.
Now, the problem with this is, how do I code the click events? Also, I have represented the board using a 2dimensional array. But how do I update the positions after every move? And how do I check whether legal move is being executed? One more thing I want to add is whenever a piece is clicked, the possible legal squares should be highlighted.
For this, what I did was, for every click event on any square it checked if there was a piece. If there was a piece, then it highlighted all possible moves for that piece using If-Else-If logic, but it turned out to be too cumbersome and too long. And another problem which arose was, how do I know that if the user clicks an empty square to move the piece there?
Please help me.

"But how do I update the positions after every move?"
When the piece is about to be moved:
Check if the piece can move like that. (Like bishop can only move diagonally)
Check that your teams piece is not on the position your about to move.
Make a second array where the move is already happened.
Check if the move was legal. (Its your turn but the your king is kill-able with one move)
To check this you need:
To get your kings position
And then look up all the possible moves for the opponent
check if a move can land on your kings position.

This question is not really a good fit for Stack Overflow, but I will give you a pointer:
You need to create a class called ChessPiece
This will have properties like IsWhite and LocationX and LocationY that stores its current position on the board and PieceType which will be an enumeration like this:
Public Enum ChessPieceType
King
Queen
Bishop
Pawn
'etc
End Enum
You need to create some sort of array of these classes so that you have one for each piece on your board and you can set these positions manaully when the game starts.
When the user attempts to move a piece you check the PieceType enumeration to make sure that type of chess piece can actually move there, then check there isn't one of their own pieces on that square etc etc.

Related

How to create the land (hills) like iOS game 'Contre Jour'?

How to create the land (hills) like iOS game Contre Jour? (Using Box2d and OpenGL)
My ideas:
Physics (Box2d)
I think we have array of bodies or fixture.
When we to touch screen, determine touch location.
If the touch location is not far from land, we begin to scan the array of bodies, and are looking for a body with coordinates closest to touch Location.
When case a touch Move, move the right body to a new coordinate (body->SetTransform(...)).
What do you think, efficient to use a large number of bodies? And find for the right body by coordinates?
Graphics (OpenGL)
There is an array of vertices and triangles created by drawing the land (hills)?
Is this true?
You can use the function b2World::QueryAABB to get a list of the fixtures in a given area, then check those for the best option. The Box2D testbed does this to find out which fixture to grab with the mouse so you could check out that source code. See also: http://www.iforce2d.net/b2dtut/world-querying
To move the body you can indeed use SetTransform, which would be good if the object does not need to interact with anything along the way. Another option might be to SetLinearVelocity to a velocity that will move the body to the dragged-to point in one time step. This is a better method if you want a continuous drag with the object being able to bump into things as it moves, because it does not teleport the body instantly to the finger position. If the body is a bullet body then it also prevents the user from dragging things through other objects, eg a static wall. Remember to set the velocity to zero when the finger is lifted :)

How to code a random movement in limited area

I have a limited area (screen) populated with a few moving objects (3-20 of them, so it's not like 10.000 :). Those objects should be moving with a constant speed and into random direction. But, there are a few limitation to it:
objects shouldn't exit the area - so if it's close to the edge, it should move away from it
objects shouldn't bump onto each other - so when one is close to another one it should move away (but not get too close to different one).
On the image below I have marked the allowed moves in this situation - for example object D shouldn't move straight up, as it would bring it to the "wall".
What I would like to have is a way to move them (one by one). Is there any simple way to achieve it, without too much calculations?
The density of objects in the area would be rather low.
There are a number of ways you might programmatically enforce your desired behavior, given that you have such a small number of objects. However, I'm going to suggest something slightly different.
What if you ran the whole thing as a physics simulation? For instance, you could set up a Box2D world with no gravity, no friction, and perfectly elastic collisions. You could model your enclosed region and populate it with objects that are proportionally larger than their on-screen counterparts so that the on-screen versions never get too close to each other (because the underlying objects in the physics simulation will collide and change direction before that can happen), and assign each object a random initial position and velocity.
Then all you have to do is step the physics simulation, and map its current state into your UI. All the tricky stuff is handled for you, and the result will probably be more believable/realistic than what you would get by trying to come up with your own movement algorithm (or if you wanted it to appear more random and less believable, you could also just periodically apply a random impulse to a random object to keep things changing unpredictably).
You can use the hitTest: method of UIView
UIView* touchedView=[self.superview hitTest:currentOrigin withEvent:nil];
In This method you have to pass the current origin of the ball and in second argument you can pass nil.
that method will return the view with which the ball is hited.
If there is any hit view you just change the direction of the ball.
for border you can set the condition for the frame of the ball if the ball go out of the boundary just change the direction of the ball.

Using a control to direct where a variable gets set

I'm just starting out, still working on improving my little poker calculator. What I'm aiming to do is have the controls on the right (green) set the float value of the middle textboxes. The hitch is that I want to do it to only the textbox that the red arrow is at. On the flipside if I move the red arrow I want it to reset the green arrow to whatever is in the textboxes where red stops.
Hope that makes sense.
I have action methods for all of the sliders and red returns exactly a value from 1-8 depending on where it stops (Currently 1)
The textboxes are all named call_1, call_2 etc for the time being but I don't know how make a method declaration for anything but exactly the name I need. Because the Red Arrow returns 1-8 that _1, _2 etc would be the way to go. It would also be nice to use this for loop counts as well in a few places.
http://img833.imageshack.us/i/screenshot20110319at723.png/
Thanks guys, I really really appreciate everything Overflow has done so far!
Cheers
Graham
Edit :
What I am trying to do is with the textboxes is to use the red slider which gives me an int of X (1-8) in this fashion.
[call_x setFloatValue:someValue]
As X changes the destination changes as well. If I can get help with this I can manipulate it to do everything else I want.
I don't fully understand what you are trying to accomplish but from looking at your screenshot I would bet that key-value observing could help you. It looks like you are dealing with the copious amounts of glue code that ends up cluttering up controller classes. This glue code manages updating your model when a view changes, and vice versa. It also defines the logic among views (i.e update a text box when a slider changes etc).
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html
Key-Value observing allows you to receive a callback when some value in another object changes. In this callback you update anything else that needs it. Taking this one step further is bindings in cocoa. Bindings literally bind the property of one object to the value of another. You should investigate these technologies to reduce the amount of boring glue code you have to manage yourself.

Objective-C draw a path and detect when it closes (forms a closed shape)

I'm fairly new to game programming (but not to programming) and I want to create a space ship which leaves a trail on the screen. Now my problem is to come up with a solution how to detect if the trail left from the ship forms a closed shape - eg. if the ship left a trail around an object, the object is caught inside its trail so to speak.
The direction I'm thinking is to draw the path of the trail on an image not visible on the screen and every now and then try to fill it with certain color and then check if fill is caught within the trail path. However it seems like a lot of overhead.
Any ideas how to do that? I'm using cocos2d if that's of any help
In game programming you often need to think more mathematically than visually.
First does your ship continuously leaves a trail on the screen? If yes, then it will be easier to know when the shape closes : you just have to remember the coordinate where your ship started to leave a trail, then wait for the trail to approach this coordinate another time (for example within a radius of 10 pixels, or else the user will need to be really accurate to hit exactly the same pixel to close the shape).
The visual representation of the trail is only here for the user, you'll never use it to compute anything. What you will do is to keep in memory the path followed by the ship's trail : a polygon, which is nothing else than the list of coordinates it followed.
Then after you know that your shape is closed, you have to determine if an object is inside your polygon or not. It's possible that objective-c or cocos2d (I don't know much about it) already contains a built-in function to know if a point is inside a polygon. In java there is the Polygon class which makes this really easy. If you don't find anything you can do it yourself, there are already great answers about this subject on SO, here is a nice one : How can I determine whether a 2D Point is within a Polygon?

Bullet Physics Problems

For those of you who have used bullet physics...
I read and ran the hello world example http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World,
and I am confused where to go next.
The hello world tutorial consisted of a btStaticPlaneShape and a btSphereShape, both rigid bodies. The sphere bounced on the static plane shape no problem.
However, I when I make another sphere at a different position, Bullet does not record collisions between the two sphere shapes, but it both automatically bounce off of btStaticPlaneShape. What kind of internal magic causes the btStaticPlaneShape to automatically bounce objects that collide with it?
Is there a setting in Bullet that automatically bounces objects off of each other after colliding? Or do you have to manually test for collisions and apply the resulting forces yourself?
Thanks.
You may have inadvertently created the spheres in a state where Bullet doesn't think they're supposed to be able to collide with each other. If you stick mostly to the defaults, and just add another sphere to the Hello World program, Bullet should notice and react to their collisions. They won't actually bounce unless you also modify Hello World to set their restitution to something greater than zero, but they will collide. For example, I added a second sphere directly above the first (by putting a for loop around the code block that creates the sphere, and using the loop variable to determine the origin y value) and extended the simulation so it runs long enough for them both to reach the plane. The first lands on the plane and rests there, the second lands on the first and rests there.
If this doesn't help, then posting some of your code is probably a good next step.