Giter Site home page Giter Site logo

tic-tac-toe-game's Introduction

.NET Windows Forms TicTacToe 1 Player Game With Unit Testing

TIC TAC TOE GAME PLAYER VS COMPUTER

Language – IDE :C# - Visual Studio 2017
Framework : .NET Windows Forms
Helpers : Resharper Ultimate Test Coverage Tool :Jetbrains dotCoverage2020.1.2

Introduction

I have created the game that Tic Tac Toe.The game against player and computer. Purpose of the game is to make a combo with owned tokens.Player’s token is Cross(X) and Computer’s token is Ought(O).Combo means that when this tokens three side by side (horizontal)or three in a row (vertical) or at three crosswise(diagonal).The game starts with player(X) after that game continue in order.If neither win then game will finish scoreless.Moreover we have option for computer’s difficulty which are easy,medium and hard.Player can play each of them.

image

  • Start Form : Program starts this form firstly and in this form we have a radio buttons for selecting difficulty level.Radio buttons must inside of the group box because just one of the radiobutton can be clicked,group box act like a container.In this form class,we have a difficulty field and this variable provides to transfer difficulty level MainForm.So we need check event methods for difficulty.Also we have button for playing the game.If this button has clicked than MainForm to be called. I’am going to tell about main form after the classes.

  • GameDifficulty Enum Class: We have talk about difficulty in the previous class and I want to create enum class for difficulty.We don’t need difficulties’ values so I want to make a enum.Because I believe that enums increases readability and comprehensible.

  • TokenType Enum Class: In this class we have 3 token type and I want to use enums for this types.Because we don’t need to token’s values.We will use for checking and control order.These types are Cross(x),Ought(O) and Empty(?).

  • Board Class: Board class related with the the game’s table.We have 2 properties in this class those are buttonMatrix and buttonList.The reason why I have created two collection to store same data is that 2D matrix has advantages to access the data and list has different advantages.For example I should access for checking winner on the buttonMatrix because I can use loop for matrix but I can not use for the generic list.And I have a named constant ROW_SIZE.After the fields we have 3 methods for checking game winners.We should check every move in the game.These methods are checking cross’ wins,checking ought’wins and checking quits for scoreless game.If every button is disable then the game will finish scoreless.

  • ComputerLogic Class: Computer Logic Class is the computer’s choice for the game and this class is static.W e have 5 static methods but ComputerPlacement method is the main method.In this method we combine other methods result’s.Firstly ,CreateAvailableButtons method is create a list that full of available buttons.Secondly,RandomButtonToAdd method creates random index between 0 and availabe buttons count in the list and return the button in that index.Thirdly,SelectOffensiveButtonToAdd method is provides to computer plays offensive actually this method for the medium level difficulty.After that SelectDefensiveButtonToAdd method is ensures that computer plays defensive.Finally we combine other methods results in ComputerPlacement.In the beginning of this method,creating button object for button to add then available list from the CreateAvailableMethod,after the worst possibility we assign buttontoadd a random button on the list using RandomButtonToAdd method.If game’s difficulty is easy then function return this button.For medium and hard difficulties we can continue.The most effective game move is the select middle button.This move is 2x more effective other selecting moves and now ways of the game divided into two possible process.If difficulty is medium then SelectOffensiveButtonToAdd method works and computer plays offensively,If difficulty is hard then SelectDefensiveButtonToAdd method works and computer plays defensively,it is realy hard because computer will not play for winning.It is going to play to make sure you don’t win. Probably it will reach its purpose.Generally this method returns a button.

  • MainForm : Finally in this class we will assemble all the pieces we have created.Also we need to create properties to store score of player and computer.Our properties are tokentype,board,playerWins,computerWins and difficulty.Moreover I use named constants in this class.Moving the methods,firstly we have a constructor.Constructor calls Reset method and initializeComponent and after Reset method.Reset Method actually can use two purpose these are Initialize and restart the game.Then loadButtons method load buttons from the form to the board object.Edit buttons as the name suggest edit buttons.ComputerMoverTick timer’s tick event method for computer moves.ComputerMoves event is used ComputerLogic class completely and complete necessary actions.Player_Click event method is works when player click button, and complete necessary actions.After that in the ResetGame method can reset and initialize game.Restart_Button_Click method provides to run ResetMethod when clicked Restart button.In WriteScore_Label method as the name suggest write score on label.ResetScore_Button_Click event reset game score.ChangeActionButtonsColor like edit buttons.CheckWins method is united with in board’s check methods and process accordingly.

https://www.youtube.com/watch?v=BjpjtBPUgaU&t=80s

tic-tac-toe-game's People

Contributors

mutabay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.