c# - Creating,using and comparing elements within a button collection -


i'm making tic-tac-toe project, , i'm having difficulties. i've gotten loose grip collections, can see problem.

thing is, created 9 buttons change background image when clicked, , disable themselves. i've managed make run 2 players, wish create sort of ai.

i need button collection can compare properties, , avoid usual one-by-one comparison , massive , numerous if statements.

what want able test whether buttons in same row or column have same values (background images). i've used far array of strings describes sign value, it's entirely detached buttons, , take lot of time type out code.

if cannot done way imagined it, please tell. open suggestions, , grateful.

oh, , if need code, or further detail, let me know.

you need separate data (array) , presentation (buttons).

update

i published sample console project compiles , runs.
model separate presentation can take tictactoe.cs , write gui it.

enter image description here

you don't need strings; booleans fine 2 states.
in fact, there third state, empty, can use nullable boolean.

so x correspond true, o false, , empty space null.
i'd create class encapsulates nullable boolean square array:

class tictactoe {     const int length = 3;     private bool? [][] _data;     private bool? _winner;      public tictactoe ()     {         _data = enumerable             .range (0, length)             .select (_ => new bool? [length])             .toarray ();     } } 

then i'd represent rows, columns , diagonals vectors:

public bool? getcell (int row, int column) {     return _data [row][column]; }  public ienumerable<bool?> getrow (int index) {     return _data [index]; }  ienumerable<int> getindices () {    return enumerable.range (0, length); }  public ienumerable<bool?> getcolumn (int index) {     return getindices ()         .select (getrow)         .select (row => row.elementat (index)); }  public ienumerable<bool?> getdiagonal (bool ltr) {     return getindices ()         .select (i => tuple.create (i, ltr ? : length - 1 - i))         .select (pos => getcell (pos.item1, pos.item2)); }  public ienumerable<ienumerable<bool?>> getrows () {     return getindices ()         .select (getrow); }  public ienumerable<ienumerable<bool?>> getcolumns () {     return getindices ()         .select (getcolumn); }  public ienumerable<ienumerable<bool?>> getdiagonals () {     return new [] { true, false }         .select (getdiagonal); }  public ienumerable<ienumerable<bool?>> getvectors () {     return getdiagonals ()         .concat (getrows ())         .concat (getcolumns ()); } 

then i'd write function takes vector , says if it's winning one:

static bool? findwinner (ienumerable<bool?> vector) {     try {         return vector             .distinct ()             .single ();     } catch (invalidoperationexception) {         return null;     } }  static bool? findwinner (ienumerable<ienumerable<bool?>> vectors) {     return vectors         .select (findwinner)         .firstordefault (winner => winner.hasvalue); }  public bool? findwinner () {     return findwinner (getvectors ()); } 

now can call getwinner find out if won.
i'd write method make move:

public bool makemove (int row, int column, bool move) {     if (_winner.hasvalue)         throw new invalidoperationexception ("the game won.");      if (_data [row][column].hasvalue)         throw new invalidoperationexception ("this cell taken.");      _data [row][column] = move;     _winner = findwinner ();      return move == _winner; }  public bool? winner {     { return _winner; } } 

this inside tictactoe class.
gui should create , call methods.

when button gets clicked, may do:

private tictactoe _game = new tictactoe (); private button [][] _buttons = new button [][3];  const bool humanplayer = true; const bool aiplayer = false;  public void handlebuttonclick (object sender, eventargs e) {     // assuming put tuple row , column in button's tag property     var position = (tuple<int, int>) ((button) sender).tag;     var row = position.item1;     var column = position.item2;      // sanity check     debug.asset (sender == _buttons [row][column]);      bool won = _game.makemove (row, column, humanplayer);      if (won) {         messagebox.show ("you won.");     }      refreshbuttons (); }  void refreshbuttons () {     (var = 0; < 3; i++) {         (var j = 0; j < 3; j++) {             var btn = _buttons [i][j];             var cell = _game.getcell (i, j);              btn.enabled = !cell.hasvalue;             btn.text = cell.hasvalue                 ? (cell.value ? "x" : "o")                 : string.empty;         }     } } 

your ai should call makemove , calculations based on information calling getrow, getcolumn , getdiagonal.

i didn't check code, it's sketch. (but console project should run fine.)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -