Making a game of checkers....help please

Basically…for classwork we made the board and that is it. So for homework, I need to make it so the mouse can select individual checkers (which are not made), and then click again to move them to a space that the rules of checkers allows.

Um…I am lost. THIS is what I have done:

var protosquare : GameObject;
//var squares1d : GameObject[ ];
var squares2d : GameObject[,];
var boardSize : int = 8;
var color1 : Color = Color.yellow;
var color2 : Color = Color.green;

function MakeBoard (nsquaresOnSide : int) {
var i : int;
var j : int;
squares1d = new GameObject[nsquaresOnSide * nsquaresOnSide];
squares2d = new GameObject[nsquaresOnSide, nsquaresOnSide];
for (i = 0; i < nsquaresOnSide; i++) {
for (j = 0; j < nsquaresOnSide; j++) {
var s : GameObject;
s = Instantiate(protosquare, Vector3(j, i, 0), Quaternion.identity);
s.transform.parent = gameObject.transform;
// s.renderer.material.color = ((i + j) 1) ? color1 : color2;
s.renderer.material.color = ((i + j) % 2) ? color1 : color2;
squares1d[i * nsquaresOnSide + j] = s;
squares2d[i, j] = s;
}
}
// squares1d[nsquaresOnSide + 3].renderer.material.color = Color.cyan;
// squares2d[1, 2].renderer.material.color = Color.black;
}

function Awake(){
// MakeBoard(boardSize);
// Debug.Break();
}

function Start(){
MakeBoard(boardSize);
// Debug.Break();
}

Now how the heck do I use this classwork to do a completely different task other than making an object? It’s like they give me code for one task and then ask me to do a completely different task unrelated to what we did. Asking here is the only thing I could think of. Sorry but you will likely see a lot of me in the next couple of months.

EDIT: And I still don’t know why I couldn’t just import a board image to put on a thin cube or make a 3D model and import that. The board creation is NOT the hard part of coding a game of checkers.

No, I cannot for the life of me imagine why they would put you to all that trouble at all.** Seriously, why bother going to class if they are going to make you figure stuff out for yourself.

Checkers
http://www.codeproject.com/KB/game/learning_draughts.aspx
http://oldschooldotnet.blogspot.com/2009/07/checkers-rules-engine-in-c.html
http://stackoverflow.com/questions/3575483/checkers-draughts-using-minimax-in-c

Selecting objects
http://answers.unity3d.com/questions/12322/drag-gameobject-with-mouse.html
http://forum.unity3d.com/threads/27759-Any-scripts-for-Touch-to-select-object-drag-to-move-object
http://answers.unity3d.com/questions/119098/how-to-drag-gameobject-with-mouse.html

Because then you don’t need to grasp three other concepts and can just use the individual objects that make up the board to detect where you clicked.

**They gave you the code to create your checkers.

Sarcasm aside, thank you. Question: if I make it so it auto deletes every even (or odd) checker…will the rest shift over or change them so they become the even ones…hence getting deleted themselves?

And we never went over why or what half the code even means in class so sorry if I don’t comprehend it. We literally spend 2 and a half hours just checking typos because the teacher kept going, “now why am I getting errors.” We watched him type it, get it to work, then end of class. That’s it. I didn’t learn anything. I stared at a projector image on a wall.

Not sure I follow.

OK…if I were to say use the modular division to add spheres above the cubes like if i + j % 2 = 1 then instantiate…actually I think that work around will do. No need to see if they are even or odd themselves, just the cube. Although I will end up with checkers on all odd spaces instead of 1 row and stopping. I likely need a < 8 in that if statement.

Although with the code I’ve made thus far, anything with it would move to the mouse click. How would I go around having the first click select an object and the second moving it? Right now whenever I click all objects move to it. I could probably rename an object to “selected object” when onMouseDown…but will it revert back to normal after my second click?

Yeah this conversation is more help than the class actually.

Untested code:

import System.Collections.Generic;

var redCheckers : List.<GameObject>;
var blackCheckers : List.<GameObject>;

function MakeCheckers(nsquaresOnSide : int, side : int)
{
    var checkers : List.<GameObject> = new List.<GameObject>();

    var row : int;
    var col : int;
    for (row = 0; row < 2; row++)
    {
        for (col = 0; col < nsquaresOnSide; col++) 
        {
            var c : GameObject;
            c = Instantiate(protoChecker, Vector3(col, (side*(nsqauresOnSide-1))-row, 0), Quaternion.identity);
            c.transform.parent = gameObject.transform;
            c.renderer.material.color = (side==0) ? color1 : color2;
            checkers.Add(c);
        }
    }
    
    return checkers;
}

function Start()
{
    MakeBoard(boardSize);
    redCheckers = MakeCheckers(boardSize, 0);
    blackCheckers = MakeCheckers(boardSize, 1);
}

Lol, justins dark humor strikes again i see… i wonder , VRaptor, do you go to school here in Canada, cause that sounds kinda alot like the way teachers here teach things O.o … money grabbin ufn … mhh… yea , enough o that. Suppose i cant blame em to hash , seems thats the story of everyones life these days… ugh!

Delaware. United States.

Basically the tech industry doesn’t exist here. We are trying basically to get into intro level jobs in game design (I’m hoping people like my art and storytelling are much better than my coding) in New York out of this.

EDIT: And thank you for the code. I’ll likely get to it on Wednesday. I have work from 12:45 to 8:45 tomorrow.

OK, so I have code now to make the checkers. Why is it not working? I get are 4 sphere (I want them on the correct spaces so I destroy every other)…but those 4 spheres are all on top of each other and inside the cubes, even though I say to move them one unit away from the cube when spawning them.

var sphere : GameObject;
var spherecolor : Color = Color.red;
var spheres2d : GameObject[,];
var sphereSize : int = 8;

function MakeSpheres (nspheresOnSide : int) {
var x : int;
var y : int;
spheres2d = new GameObject[nspheresOnSide, nspheresOnSide];
for (x = 0; x < nspheresOnSide; x++) {
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
var s2 : GameObject;
s2 = Instantiate(sphere, Vector3(x, y, 1), Quaternion.identity);
s2.transform.parent = gameObject.transform;
s2.renderer.material.color = spherecolor;
spheres2d[x, y] = s2;

if ((y % 2 == 1) (y <= 8)){
Destroy (sphere);
}
}
}
}
}

function Start(){
MakeSpheres(sphereSize);
}

Actually now Unity keeps crashing when I try to play. What am I doing wrong?

OK, I fixed the crashing. Now I have back down to 4 spheres, but once agin they are all on top of each other and still somehow inside the cubes.

var sphere : GameObject;
var spherecolor : Color = Color.red;
var spheres2d : GameObject[,];
var sphereSize : int = 8;

function MakeSpheres (nspheresOnSide : int){
var x : int;
var y : int;

spheres2d = new GameObject[nspheresOnSide, nspheresOnSide];
for (x = 0; x <= 3; x++) {
var s2 : GameObject;
if ((y % 2 == 1) (y <= 8)) continue;
s2 = Instantiate(sphere, Vector3(x, y, -1), Quaternion.identity);
s2.transform.parent = gameObject.transform;
s2.renderer.material.color = spherecolor;
spheres2d[x, y] = s2;
}
}

function Start(){
MakeSpheres(sphereSize);
}

I understand that you are just a beginner in programming, but I also get the feeling that you are not actually trying to really understand the code and what each piece of it does on a fundamental level. You have certainly gotten the idea of how certain parts of it work, but are overlooking how the jigsaw should go together.

To fix the “on top of each other” issue, experiment with the values in the Vector3.
To fix the “inside the cubes” issue, again, experiment with the values in the Vector3.

Yeah…thing is, no matter WHAT I put inside the vector 3, it all goes to (0, 5, 1). That should not happen. All that changes is how many are in that spot. That is an error I did not expect. I emailed the teacher my exported project to see if he has any idea why.

Honestly…I liked my triple loop idea. I thought I solved that one. In theory it should have worked. One to decide how many X length it is, another for Y (so you don’t get checkers in every column), and the third to remove every odd sphere so they end up on the correct squares. But through emails with the instructor…the triple nested loop is gone.

I look at the code above and still go…that should work. I don’t see the error in logic. I make the size the same as the board, then make it so I spawn checkers one unit away from the board, and ignore every other one so only half get made, and automatically stop it once more than 4 of them appear (well it does do that part but they are all in the same spot).

How are you getting on?

I’m officially an idiot. The problem I had was that I had a second script also on the prefab that I forgot to uncheck. It was there for smoothing motion after I get the selection process down. My most recent code was actually correct with a minor number change. All this time I was trying to correct the wrong code.

Well…now off to selection and THEN attaching the smoothing.

OK Now I’m working on selecting the 4 checkers. What I have so far is:

var startTime : float;
var steps : float = 5;

function OnMouseClick (){
if (gameObject.sphere == true){
var targetsphere = this;
}
if (gameObject.cube == true){
var targetcube = this;
var location = Vector3(targetcube.position.x, targetcube.position.y, targetcube.position.z - 1);

var dist = Vector3.Distance(transform.position, location);
var s = Mathf.SmoothStep(0, dist, (Time.time - startTime) / steps);
targetsphere.transform.position = Vector3.Lerp(transform.position, location, s);
}
}

Now I’m pretty sure the whole “false” and “this” stuff is not what I want. What I am attempting to do is have it so you click any of the checkers, and that one checker becomes the selected one. Then when you click on a cube, the selected sphere moves to that cube’s location (minus 1 Z in order to make it visible). Then when you click on another cube or sphere, that one becomes the selected one and the previous cube/sphere is no longer selected.

Right now I have a situation where nothing moves. I just recently added

print(“you have picked a checker”);
print(“you have picked a square”);

under the ifs and nothing shows so something tells me it isn’t even getting to the if statements. I’m thinking the == true is not what I want to use.