Adam

Adam

  • NA
  • 131
  • 30.7k

Checkergame continuing

Apr 27 2011 6:09 AM
So here is code for .exe and below it for dll. When u start it, and press enter on some symbol and move it across another, the symbol u have moved across, disappear.

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

namespace Checkers
{
    class Checkers
    {
       
        static Saveboard Board;
        static char[,] Area;
        static char[,] Area2;

        static void DrawBoard(bool firstTime)
        {
      //only need to do some things the first time the board is drawn

      const int X = 8;
      const int Y = 8;
      int num = 8;

      if (firstTime) Console.Clear();
    
      // save cursor position
      int origRow = Console.CursorTop;
      int origCol = Console.CursorLeft;

      // reset to 0,0
      Console.SetCursorPosition(0, 0);

      for (int i = 0; i < X; i++)
      {
         for (int y = 0; y < Y; y++)
         {
             Console.Write(Area[i, y]);
         }
         if (firstTime)
         {
             Console.WriteLine(num);
             num--;
         }
         else
         {
            Console.WriteLine();
         }
      }
  
      if (firstTime) Console.WriteLine("ABCDEFGH");

      // restore cursor postion
      Console.CursorTop = origRow;
      Console.CursorLeft = origCol;
   }

        public static void Keys()
        {
            bool redraw = false;
            int col = 0;
            int row = 0;

            while (true)
            {
                var ch = Console.ReadKey().Key;

                switch (ch)
                {

                    case ConsoleKey.RightArrow:

                        Area = Board.MoveRight(out col, out row, out redraw);
                        break;

                    case ConsoleKey.DownArrow:

                        Area = Board.MoveDown(out col, out row, out redraw);
                        break;

                    case ConsoleKey.LeftArrow:

                        Area = Board.MoveLeft(out col, out row, out redraw);
                        break;
                    case ConsoleKey.UpArrow:

                        Area = Board.MoveUp(out col, out row, out redraw);
                        break;

                    case ConsoleKey.Enter:

                        Area = Board.EnterPressed(out col, out row, out redraw);
                        break;
                }


                if (redraw) DrawBoard(false);
                Console.SetCursorPosition(col, row); // in case it's not in the right position following the move
            }
        }


        static void Main()
        {
            Board = new Saveboard();
            Area = Board.InitializeBoard();
            Area2 = Board.Checkerboard();
            DrawBoard(true);
            Console.ReadKey(true);
            Keys();
        }
    }
}

Code for dll:

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

namespace ClassLibrary1
{
    public class Saveboard
    {
        const int X = 8;
        const int Y = 8;
        char[,] Area = new char[X, Y];
        char[,] Area2 = new char[X, Y];
        int switcher = 0;
        char Symbol = (char)5;
        char Symbol2 = (char)6;
        bool pick = true;
        int xpos = 0;
        int ypos = 0;

        public char[,] InitializeBoard()
        {

            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, 1] = Symbol;
            Area[0, 3] = Symbol;
            Area[0, 5] = Symbol;
            Area[0, 7] = Symbol;
            Area[1, 0] = Symbol;
            Area[1, 2] = Symbol;
            Area[1, 4] = Symbol;
            Area[1, 6] = Symbol;
            Area[2, 1] = Symbol;
            Area[2, 3] = Symbol;
            Area[2, 5] = Symbol;
            Area[2, 7] = Symbol;
            Area[4, 1] = Symbol2;
            Area[4, 4] = Symbol2;
            Area[5, 2] = Symbol2;

            return Area;
        }

        public char[,] Checkerboard()
        {
            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++;
            }
            return Area2;
        }

        public char[,] MoveRight(out int col, out int row, out bool redraw)
        {
            if (xpos + 1 == X)
            {            
                redraw = true;
            }
            else if (pick)
            {
                xpos++;
                redraw = true;
            }
            else
            {
                Area[ypos, xpos] = Area2[ypos, xpos];
                xpos++;               
                redraw = true;
                Area[ypos, xpos] = Symbol;
            }
            col = xpos;
            row = ypos;
            return Area;
            }

        public char[,] MoveDown(out int col, out int row, out bool redraw)
        {
            if (ypos + 1 == Y)
            {
                redraw = true;
            }
            else if (pick == true)
            {
                ypos++;
                redraw = true;
            }
            else
            {
                Area[ypos, xpos] = Area2[ypos, xpos];
                ypos++;;
                redraw = true;
                Area[ypos, xpos] = Symbol;
            }
            col = xpos;
            row = ypos;
            return Area;
        }

        public char[,] MoveLeft(out int col, out int row, out bool redraw)
        {
            if (xpos - 1 < 0)
            {
                redraw = true;
            }
            else if (pick == true)
            {
                xpos--;
                redraw = true;
            }
            else
            {
                Area[ypos, xpos] = Area2[ypos, xpos];
                xpos--;
                redraw = true;
                Area[ypos, xpos] = Symbol;
            }
            col = xpos;
            row = ypos;
            return Area;
        }

        public char[,] MoveUp(out int col, out int row, out bool redraw)
        {
            if (ypos - 1 < 0)
            {
                redraw = true;
            }
            else if (pick == true)
            {
                ypos--;
                redraw = true;
            }
            else
            {
                Area[ypos, xpos] = Area2[ypos, xpos];               
                ypos--;
                Area[ypos, xpos] = Symbol;
                redraw = true;
            }
            col = xpos;
            row = ypos;
            return Area;
        }

              public char[,] EnterPressed(out int col, out int row, out bool redraw)
              {
                  if (pick == true && Area[ypos, xpos] == Symbol)
                  {
                      redraw = false;
                      pick = false;
                  }
                  else if (pick == false && Area2[ypos, xpos] != Symbol)
                  {
                     
                      redraw = true;
                      Area[ypos, xpos] = Symbol;
                      pick = true;
                  }
                  else
                  redraw = false;
                  col = xpos;
                  row = ypos;
                  return Area;
              }
        }
    }

Answers (37)