1 year ago

This works fine but, how can i do it in less code?

( tic-tac-toe)

the code is given in the article


i was making a tic tac toe game

here is the code

      /*tic tak toe */

               

       

       

    bool wannaPlay = true;                while(wannaPlay){
            char [] pos = new char[9];             pos[0] = ' ';            pos[1] = ' ';            pos[2] = ' ';            pos[3] = ' ';            pos[4] = ' ';            pos[5] = ' ';            pos[6] = ' ';            pos[7] = ' ';            pos[8] = ' ';            Console.WriteLine("This is a tic tac toe game:");            Console.WriteLine("\nenter numbers from 1 to 9");            bool xsturn = true;            int i = 0;                    while(i<9){                            if(xsturn){                    Console.Write("\nplayer 1's turn (X): ");                    xsturn = false;                }                else if(!xsturn){                    Console.Write("\nplayer 2's turn (O): ");                    xsturn = true;                }                string input;                int num = 0;                                do{   input = Console.ReadLine();                    num = Convert.ToInt32(input);                    if((num < 1 || num >9)){                        Console.WriteLine("\nwrong input. enter again");                    }                }                while((num < 1 || num >9));
                if(pos[num-1] == ' '){                    if(!xsturn){                        pos[num-1] = 'X';                    }                    else if(xsturn){                        pos[num-1] = 'O';                    }                }                else{                                        Console.WriteLine("\ntaken");                    xsturn = !xsturn;                    continue;                }
                                                        Console.WriteLine("\n {0} | {1} | {2} ",pos[0],pos[1],pos[2]);//3                Console.WriteLine(" --------- ");                Console.WriteLine(" {0} | {1} | {2} ",pos[3],pos[4],pos[5]);//12                Console.WriteLine(" ――――――――― ");                Console.WriteLine(" {0} | {1} | {2} ",pos[6],pos[7],pos[8]);//21                                                if(pos[0] == 'X' && pos[1] == 'X' && pos[2] == 'X' ||                pos[3] == 'X' && pos[4] == 'X' && pos[5] == 'X' ||                pos[6] == 'X' && pos[7] == 'X' && pos[8] == 'X' ||                pos[0] == 'X' && pos[3] == 'X' && pos[6] == 'X' ||                pos[1] == 'X' && pos[4] == 'X' && pos[7] == 'X' ||                pos[2] == 'X' && pos[5] == 'X' && pos[8] == 'X' ||                pos[0] == 'X' && pos[4] == 'X' && pos[8] == 'X' ||                pos[2] == 'X' && pos[4] == 'X' && pos[6] == 'X'  ){                    Console.WriteLine("Player 1 (X) won!");                    break;                }                else if(pos[0] == 'O' && pos[1] == 'O' && pos[2] == 'O' ||                pos[3] == 'O' && pos[4] == 'O' && pos[5] == 'O' ||                pos[6] == 'O' && pos[7] == 'O' && pos[8] == 'O' ||                pos[0] == 'O' && pos[3] == 'O' && pos[6] == 'O' ||                pos[1] == 'O' && pos[4] == 'O' && pos[7] == 'O' ||                pos[2] == 'O' && pos[5] == 'O' && pos[8] == 'O' ||                pos[0] == 'O' && pos[4] == 'O' && pos[8] == 'O' ||                pos[2] == 'O' && pos[4] == 'O' && pos[6] == 'O'){                    Console.WriteLine("Player 2 (O) won!");                break;                }                else if(i==8){                    Console.WriteLine("\ndraw");                }                i++;                        }            Console.Write("\nDo you want to continue playing (Y/N):");            string playOrNot = Console.ReadLine();            if(playOrNot == "N" || playOrNot == "n"){                break;            }            }             Console.WriteLine("\nhave a nice day!");                            }



3 comments

Loading...

Next up

firing animation added.

Bullets & Fire Effects added.

MAKE SURE TO FOLLOW THE GAME "Agent XYZ"

https://gamejolt.com/games/Agent-XYZ/799241

A character for my top-down shooter game.

I can't do game art, this is the best I could. Lol

Happy #WIPWednesday!

Are you working on a game? A song? Something else? Tell us in the comments!

sh*t prototype. Should i try to do any further work.

🕹️ Enter The Highrise Game Jam Before It Ends On May 5! 🕹️

Learn the rules here: https://gamejolt.com/c/gamedev/highrisejam

Learn how to make a world on Highrise (which you MUST do to enter the jam) here: https://gamejolt.com/p/the-highrise-game-jam-is-underway-watch-t…

The jam has cash prizes! 💸💸💸

mental canvas is really amazing.

You have to dodge the enemies and defuse the bomb in given time (here 20 sec).

Hold space bar to defuse the bomb.

Make sure to follow the game.

https://gamejolt.com/games/Agent-XYZ/799241

Highrise Game Jam Reminder: In order to enter to win the Jam, your game must be created in Unity and submitted using the Highrise Studio Hub.

Learn all the deets about the jam at https://gamejolt.com/c/gamedev/highrisejam

Make sure to follow the game "Agent XYZ"

https://gamejolt.com/games/Agent-XYZ/799241