vb.net - Graphic Tic Tac Toe in Visual Basic -


i'm working on program visual basic class supposed create game of tic-tac-toe played 1 human , computer.

here instructions:

  1. create 2 views (one gui view , other text view using console).
  2. use 2 controllers (one user clicks on gui , other user uses numbers 1-9 on keyboard.
  3. use 1 model.
  4. use module keep state of game in 2d array. logic applied model in module (for example, module allowed change 2d array).
  5. the grid drawn graphically (not using labels, buttons, etc)

here of questions i'm having far:

  1. i know how create gui view, have no idea how use console create text view?
  2. not quite sure how 2d array supposed work in situation (i've declared follows, not sure go there).

    module module1     dim game(2, 2) string end module 
  3. i've drawn part of grid, having trouble doing rest. need 1 more vertical line , 1 more horizontal line, , needs divided correctly. here's have drawing far:

    private sub form1_paint(byval sender object, byval e system.windows.forms.painteventargs) handles me.paint     dim blackbrush new drawing.solidbrush(color.black)     dim xbase integer = 50     dim ybase integer = 10     dim width integer = 200      e.graphics.drawrectangle(pens.green, xbase, 10, 200, 200)     dim third integer = ybase + width / 3     e.graphics.drawline(pens.black, xbase, third, xbase + width, third)     e.graphics.drawline(pens.black, 100, 5, 100, 220) end sub 

i giving half of solution in c# :d

it shouldn't hard convert vb, same framework.

hopefully, you'll understand mechanics , not dump code, it's simple in fact.

(drop picturebox onto form first)

the grid :

here i'm drawing squares should pretty easy draw lines instead.

enter image description here

public partial class form1 : form {     public form1()     {         initializecomponent();         picturebox1.paint += picturebox1_paint;     }      private void picturebox1_paint(object sender, painteventargs e)     {         int columns = 3;         int rows = 3;         graphics graphics = e.graphics;         graphics.clear(color.white);         rectanglef bounds = graphics.visibleclipbounds;         var cellwidth = (int)((bounds.width - 1) / columns);         var cellheight = (int)((bounds.height - 1) / rows);          (int x = 0; x < columns; x++)         {             (int y = 0; y < rows; y++)             {                 graphics.drawrectangle(pens.black, new rectangle(x * cellwidth, y * cellheight, cellwidth, cellheight));             }         }     }      private void form1_load(object sender, eventargs e)     {         picturebox1.invalidate();     } } 

here's simple board :

internal class tictactoe {     public tictactoe()     {         grid = new piece[3, 3];     }      public piece[,] grid { get; private set; }      public void setpiece(int x, int y, piece value)     {         if (x < 0 || x > 2) throw new argumentoutofrangeexception("x");         if (y < 0 || y > 2) throw new argumentoutofrangeexception("y");          piece piece = grid[y, x];         if (piece == piece.none)         {             grid[y, x] = value;         }     }      public piece getpiece(int x, int y)     {         if (x < 0 || x > 2) throw new argumentoutofrangeexception("x");         if (y < 0 || y > 2) throw new argumentoutofrangeexception("y");          return grid[y, x];     } }  internal enum piece {     none = 0,     cross = 1,     circle = 2 } 

you return boolean or throw exception, silently update game in example.

what's left ?

  • find if has won, can tricky implement :d really
  • draw in console mode

i'll give pseudo code console :

do   user input   update grid   check if wins   clear console   draw current game or game on screen loop until esc (quit) pressed 

for drawing grid in console it's code above except dimensions smaller , pixels characters instead.

http://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx

as said, converting should trivial since msdn docs provide vb c# examples in every of docs pages.

good luck !


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -