42
Answers

Need help with checkers game

Adam

Adam

14y
6.5k
1
Hi, I need advice for my game checkers please. Below is my code. When you pull enter you choose figurine and you can move with it on checkerboard and then save it to new position. It worked good with figurine on start position Area[0,0], but when I add new figurine on position Area[0,2] it disappear when cursor crosses it. Why it works only with figurine on position [0,0] ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Checkers
{
  class Checkers
  {
  const int X = 8;
  const int Y = 8;
  int switcher = 0;
  int num = 8;
  int xpos = 0;
  int ypos = 0;
  bool pick = false;
  char[,] Area = new char[X, Y];
  char[,] Area2 = new char[X, Y];
  char Symbol = (char)5;

  void Checkerboard()
  {
  Cursor();
  for (int i = 0; i < X; i++)
  {
  for (int y = 0; y < Y; y++)
  {
  if (switcher % 2 == 0)
  Area[i, y] = 'X';
  else
  Area[i, y] = 'Y';
  switcher++;
  Area[0, 0] = Symbol;
  Area[0, 2] = Symbol;
  Console.Write(Area[i, y]);
  }
  Console.WriteLine(num);
  num--;
  switcher++;
  }
  Console.WriteLine("ABCDEFGH");
  if (pick == false)
  Console.Write("Vyber figurku");
  if(pick==true)
  Console.Write("Vyber misto a uloz");
  }

  void Checkerboard2()
  {
  for (int i = 0; i < X; i++)
  {
  for (int y = 0; y < Y; y++)
  {
  if (switcher % 2 == 0)
  Area2[i, y] = 'X';
  else
  Area2[i, y] = 'Y';
  switcher++;
  }
  switcher++;
  }

  }

  void Cursor()
  {
  bool saveCursorVisibile;
  int saveCursorSize;

  Console.CursorVisible = true;
  saveCursorVisibile = Console.CursorVisible;
  saveCursorSize = Console.CursorSize;
  Console.CursorSize = 100;
  }


  void Keys()
  {
  while (true)
  {

  var ch = Console.ReadKey().Key;
  switch (ch)
  {

  case ConsoleKey.Enter:
  if (Area[xpos, ypos] == Symbol && pick == false)
  {
  pick = true;
  }
  if (Area[xpos, ypos] != Symbol && pick == true)
  {
  Area[xpos, ypos] = Symbol;
  pick = false;
  }
  break;
 

  case ConsoleKey.UpArrow:
  {
  if (ypos - 1 < 0)
  break;
 
  Console.SetCursorPosition(xpos, ypos);
  Console.Write(Area[xpos, ypos]);
  ypos--;
  break;
  }
  case ConsoleKey.DownArrow:
  {
  if (ypos + 1 == Y)
  break;

  Console.SetCursorPosition(xpos, ypos);
  Console.Write(Area[xpos, ypos]);
  ypos++;
  break;
  }
  case ConsoleKey.LeftArrow:
  {
  if (xpos - 1 < 0)
  break;
 
  Console.SetCursorPosition(xpos, ypos);
  Console.Write(Area[xpos, ypos]);
  xpos--;
  break;
  }
  case ConsoleKey.RightArrow:
  {
  if (xpos + 1 == X)
  break;

  Console.SetCursorPosition(xpos, ypos);
  Console.Write(Area[xpos, ypos]);
  xpos++;
  break;
  }
  }
  Console.SetCursorPosition(xpos, ypos);
  if (pick == true)
  {
  Area[xpos, ypos] = Area2[xpos, ypos];
  Console.Write(Symbol);
  Console.SetCursorPosition(xpos, ypos);
  } 
  }
  }

  static void Main()
  {
  Console.Clear();
  Checkers check = new Checkers();
  check.Checkerboard();
  check.Checkerboard2();
  Console.SetCursorPosition(0, 0);
  check.Keys();
  }
  }
}

Answers (42)
2
Kunal Naik

Kunal Naik

NA 425 0 13y
Hi,

Yes, you should build the classlibrary(businesslayer/dataaccesslayer) and then add the reference.

If you want to add class files at a later stage then you should rebuild your classlibrary and then get reference. This will not cause any problem.
Accepted
0
Nethra R S

Nethra R S

NA 310 266.8k 13y
Hi Kunal,

Should we build the classlibrary(businesslayer/dataaccesslayer) and then add the reference?

What if we want to add class files at a later stage? then what should be done? should we rebuild? will that not cause any problem?
0
Kunal Naik

Kunal Naik

NA 425 0 13y
Hi,

Build your BusinessLayer and then in your website you will access your 2 classes with the help of businesslayer object.
And if still that error comes then go to Add Refrence section under Reference section in side your web site root then under Browse tab select the dll from your projects bin folder then click ok. Now you access your these 2 classes in your website.

Hope this will helps you.
0
Nethra R S

Nethra R S

NA 310 266.8k 13y
What to check Miss Priya Linge???? You are not understanding my question itself
0
Priya Linge

Priya Linge

NA 5k 708.3k 13y
Hi,

Please check this,

  • App.Core - business layer logic services
  • App.Data - data access layer store classes and data access objects
  • App.Web - user interface layer
  • 0
    Nethra R S

    Nethra R S

    NA 310 266.8k 13y
    The BusinessLayer namespace itself and i have also mention using BusinessLayer; in the place where i wnat to use it..
    0
    Priya Linge

    Priya Linge

    NA 5k 708.3k 13y
    Hi Nethra,

    Can you please tell me which namespace you have added for accessing Businesslayer object.

    Thanks.