Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.3 kB
6
Indexable
using System;

namespace Duello
{
    class Program
    {
        /*
         * Variable notes:
         * If turn value is 0 ->> o;
         * If turn value is 1 ->> x;
         */
        static int sX=-1, sY=-1, round=1, turn=1, tX=0, tY=0;

        static void initializeBoard(char[,] board) {
            for(int i=0; i<board.GetLength(0); i++) {
                for(int j=0; j<board.GetLength(1); j++) {
                    if(i<3 && j<3)
                        board[i,j]='o';
                    else if(i>4 && j>4)
                        board[i,j]='x';
                    else
                        board[i,j]='.';
                }
            }
        }

        static void drawBoard(char[,] board, string playerName) {
            Console.Clear();
            Console.SetCursorPosition(0, 0);

            Console.WriteLine("   1 2 3 4 5 6 7 8");
            Console.WriteLine("  ╔═══════════════╗");
            for(int i=0; i<board.GetLength(0); i++) {
                Console.Write("{0}-║", i+1);
                for(int j=0; j<board.GetLength(1); j++) {
                    // Draw all row elements
                    Console.Write("{0} ", board[j, i]);

                    // Remove space for last row element
                    if(j==board.GetLength(1)-1)
                        Console.Write("\b");
                }
                
                if(i==0) {
                    Console.WriteLine("║ Round: {0}", round);
                }
                else if(i==2 && turn==0) {
                    Console.WriteLine("║ Turn: Computer");
                }
                else if(i==2 && turn==1) {
                    Console.WriteLine("║ Turn: {0}", playerName);
                }
                else if(i==4) {
                    Console.Write("║ ");
                    
                    if(sX!=-1 && sY!=-1) 
                        Console.WriteLine("Selected: {0}, {1}", sX+1, sY+1);
                    else
                        Console.WriteLine("Selected: None");
                }
                else if(i==6) {
                    Console.WriteLine("║ Cursor: {0}, {1}", tX, tY);
                }
                else {
                    Console.WriteLine("║ ");
                }
            }
            Console.WriteLine("  ╚═══════════════╝");
        }

        static void Main(string[] args)
        {
            char[,] board = new char[8,8];
            ConsoleKeyInfo key;
            int cursorX=3, cursorY=2;
            Console.Write("Please enter your name: ");
            string name = Console.ReadLine();

            initializeBoard(board);

            while(true) {
                drawBoard(board, name);
                Console.SetCursorPosition(cursorX, cursorY);
                key=Console.ReadKey();

                if(key.Key == ConsoleKey.Escape) {
                    Console.Clear();
                    break;
                }
                if(key.Key==ConsoleKey.Z && board[tX, tY]=='x') {
                    sX=tX;
                    sY=tY;
                }
                if(key.Key==ConsoleKey.X && sX!=-1) {
                    if(board[tX, tY]=='.') {
                        ++round;
                        if((sX==tX+1 && sY==tY) || (sX==tX-1 && sY==tY)
                        || (sY==tY+1 && sX==tX) || (sY==tY-1 && sX==tX)) {
                            board[tX, tY]='x';
                            board[sX, sY]='.';
                            sX=-1;
                            sY=-1;
                        }
                    }
                    
                }

                if(key.Key == ConsoleKey.RightArrow && cursorX<17) {
                    cursorX+=2;
                    ++tX;
                }
                else if(key.Key==ConsoleKey.LeftArrow && cursorX>3) {
                    cursorX-=2;
                    --tX;
                }
                else if(key.Key==ConsoleKey.DownArrow && cursorY<9) {
                    ++cursorY;
                    ++tY;
                }
                else if(key.Key==ConsoleKey.UpArrow && cursorY>2) {
                    --cursorY;
                    --tY;
                }
            }
        }
    }
}