Question: C#: How do I convert a Console Application to a WinForm? I created a Tic Tac Toe program in C# using Visual Stuido 2015, but

C#: How do I convert a Console Application to a WinForm?

I created a Tic Tac Toe program in C# using Visual Stuido 2015, but I have to convert the program to a WinForm using 9 buttons. I'm not sure how to do that. I found some tutorial online, but I believe I have to use the codes below?!

Here is the Codes for the Console application:

Form Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TicTacToe

{

class Program

{

static void Main(string[] args)

{

TTT myGame = new TTT();

Console.WriteLine("FirstPlayer is {0} token is {1} ", myGame.playerName(myGame.currentPlayer), myGame.PlayerToken(myGame.currentPlayer));

myGame.Start();

Console.ReadLine();

}

}

}

Class TTT Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TicTacToe

{

class TTT

{

string[][] Players = new string[][]{

new string [] {"Computer", "x"},

new string [] {"Human", "O"}

};

const int Unplayed = -1;

const int Computer = 0;

const int Human = 1;

Random rNd = new Random();

int _currentPlayer;

int[] GameBoard = new int[9];

int[][] wins = new int[][] { new int [] { 0, 1, 2 }, new int[] { 3, 4, 5 }, new int[]{ 6,7,8},

new int[]{0,3,6}, new int []{1,4,7},new int[] {2,5,8 },new int[] {2,4,6}, new int[] { 2,4,8}};

public int currentPlayer

{

get

{ return _currentPlayer; }

set { _currentPlayer = value; }

}

public string playerName(int player)

{

return Players[player][0];

}

public string PlayerToken(int player)

{

return Players[player][1];

}

public TTT()

{

InitializeGameBoard();

displayGameBoard();

_currentPlayer = rNd.Next(0, 2);

//_currentPlayer = 0;

}

public void InitializeGameBoard()

{

for (int i = 0; i < GameBoard.Length; i++)

GameBoard[i] = Unplayed;

}

public void displayGameBoard()

{

Console.WriteLine("{0}| {1}| {2}|", pieceAT(0), pieceAT(1), pieceAT(2));

Console.WriteLine("{0}| {1}| {2}|", pieceAT(3), pieceAT(4), pieceAT(5));

Console.WriteLine("{0}| {1}| {2}|", pieceAT(6), pieceAT(7), pieceAT(8));

Console.WriteLine();

}

string pieceAT(int boardPosition)

{

if (GameBoard[boardPosition] == Unplayed)

return (boardPosition + 1).ToString();

return PlayerToken(GameBoard[boardPosition]);

}

int getMoveFor(int player)

{

if (player == Human)

return getManualMove(player);

else {

int selectMove = getRandomMove(player);

Console.WriteLine("{0} selects poistion {1}", playerName(player), selectMove + 1);

return selectMove;

}

}

int getManualMove(int player)

{

while (true)

{

Console.Write("{0} enter Your Move(1 - 9)", playerName(player));

ConsoleKeyInfo keyinfo = Console.ReadKey();

Console.WriteLine();

if (keyinfo.Key == ConsoleKey.Escape)

return Unplayed;

if (keyinfo.Key >= ConsoleKey.D1 && keyinfo.Key <= ConsoleKey.D9)

{

int move = keyinfo.KeyChar - '1';

if (GameBoard[move] == Unplayed)

return move;

else

Console.WriteLine("Spot is already taken");

}

else

Console.WriteLine("Wake Up");

}

}

int getRandomMove(int player)

{

int movesLeft = GameBoard.Count(position => position == Unplayed);

int x = rNd.Next(0, movesLeft);

for (int i = 0; i < GameBoard.Length; i++)// sequential search

{

if (GameBoard[i] == Unplayed && x < 0)

return i;

x--;

}

return Unplayed;

}

void PlayMove(int boardPosition, int player)

{

GameBoard[boardPosition] = player;

}

bool isGameWon()

{

return wins.Any(line => takenBySamePlayer(line[0], line[1], line[2]));

}

bool takenBySamePlayer(int a, int b, int c)

{

return GameBoard[a] != Unplayed &&

GameBoard[a] == GameBoard[b] && GameBoard[a] == GameBoard[c];

}

bool isGameTied()

{

return !GameBoard.Any(Spot => Spot == Unplayed);

}

int getNextPlayer(int player)

{

return (player + 1) % 2;

}

public void Start()

{

while (true)

{

int thisMove = getMoveFor(currentPlayer);

if (thisMove == Unplayed)

{

Console.WriteLine("{0}, you've quit the game ... am I that good?", playerName(currentPlayer));

break;

}

PlayMove(thisMove, currentPlayer);

displayGameBoard();

if (isGameWon())

{

Console.WriteLine("{0} has won the game!", playerName(currentPlayer));

break;

}

else if (isGameTied())

{

Console.WriteLine("Cat game ... we have a tie.");

break;

}

currentPlayer = getNextPlayer(currentPlayer);

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!