i'm making tictac toe xna firmer grip on c# programming, can't figure out how solve problem: checkif there winning patern in boolean array player 1 , 2 , if game has stop, stops if there row of both x's , o's. however, doesn't aply when pattern vertical. the:code(i'm using few texture2d's 1 cross 1 "o" , 2 mouse forms , 1 background
using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; namespace tictactoe { public class game1 : microsoft.xna.framework.game { graphicsdevicemanager graphics; static int x, y; spritebatch spritebatch; static bool ifinitialized = false; static bool player,winone,wintwo; texture2d ttts, tttx, tttr, mousex, mouseo; texture2d[,] whatdraw; spritefont font; rectangle[,] select; vector2[,] position; mousestate mouse = new mousestate() ; mousestate prevmouse = new mousestate(); rectangle mouserect ; static bool[,] playerone, playertwo, total; public game1() { graphics = new graphicsdevicemanager(this); content.rootdirectory = "content"; } protected override void initialize() { mouse = mouse.getstate(); playerone = playertwo = total = new bool[3, 3] { {false,false,false }, { false, false, false }, { false, false, false } }; select = new rectangle[3, 3]; position = new vector2[3, 3]; whatdraw = new texture2d[3, 3]; position[0, 0] = new vector2(0, 0); position[1, 0] = new vector2(162, 0); position[2, 0] = new vector2(322, 0); position[0, 1] = new vector2(0, 162); position[1, 1] = new vector2(162, 162); position[2, 1] = new vector2(322, 162); position[0, 2] = new vector2(0, 322); position[1, 2] = new vector2(162, 322); position[2, 2] = new vector2(322, 322); x = 0; y = 0; this.ismousevisible = false; base.initialize(); } protected override void loadcontent() { spritebatch = new spritebatch(graphicsdevice); ttts = content.load<texture2d>("tttscheme"); tttr = content.load<texture2d>("tttr"); tttx = content.load<texture2d>("tttx"); font = content.load<spritefont>("font"); mouseo = content.load<texture2d>("mouse0;"); mousex = content.load<texture2d>("mousex;"); while (y<3) { while (x<3) { whatdraw[x, y] = content.load<texture2d>("empty"); x++; } x = 0; y++; } y = 0; } protected override void unloadcontent() { } protected override void update(gametime gametime) { if (gamepad.getstat(playerindex.one).buttons.back== buttonstate.pressed) this.exit(); if (!ifinitialized) { select[0, 0] = new rectangle(0, 0,tttx.width,tttx.height); select[1, 0] = new rectangle(162, 0, tttx.width, tttx .height); select[2, 0] = new rectangle(322, 0, tttx .width, tttx .height); select[0, 1] = new rectangle(0, 162, tttx .width, tttx .height); select[1, 1] = new rectangle(162, 162, tttx .width, tttx .height); select[2, 1] = new rectangle(322, 162, tttx.width, tttx .height); select[0, 2] = new rectangle(0, 322, tttx.width, tttx .height); select[1, 2] = new rectangle(162, 322, tttx .width, tttx .height); select[2, 2] = new rectangle(322, 322, tttx .width, tttx .height); ifinitialized = true; } prevmouse = mouse; mouse = mouse.getstate(); if (!winone && !wintwo) { while (y < 3) { while (x < 3) { if (select[x, y].contains(new point(mouse.x, mouse.y)) && mouse.leftbutton == buttonstate.pressed && prevmouse.leftbutton == buttonstate.released&& (total[x, y] == false)) { total[x, y] = true; if (player == false) { playerone[x,y] = true; whatdraw[x,y] = tttx; player = true; ifwona(); } else if (player == true) { playertwo[x, y] = true; whatdraw[x,y] = tttr; player = false; ifwonb(); } } x++; } x = 0; y++; } y = 0; } base.update(gametime); } protected override void draw(gametime gametime) { graphicsdevice.clear(color.white); spritebatch.begin(); spritebatch.draw(ttts, new vector2(0, 0), color.white); while(y<3) { while (x < 3) { spritebatch.draw(whatdraw[x, y], position[x, y], color.white); x++; } x = 0; y++; } y = 0; if(player==true) spritebatch.draw(mouseo, new vector2 (mouse.x-(mouseo .width /2), mouse.y-(mouseo .height/2)), color.white); if(player== false) spritebatch.draw(mousex, new vector2(mouse.x - (mousex.width / 2), mouse.y - (mousex.height / 2)), color.white); spritebatch.end(); base.draw(gametime); } static void ifwona() { if ((playerone[0, 0] && playerone[1, 0] && playerone[2, 0]) || (playerone[0, 1] && playerone[1, 1] && playerone[2, 1]) || (playerone[0, 2] && playerone[1, 2] && playerone[2, 2]) || (playerone[0, 0] && playerone[1, 0] && playerone[2, 0]) || (playerone[0, 1] && playerone[1, 1] && playerone[2, 1]) || (playerone[0, 2] && playerone[1, 2] && playerone[2, 2]) || (playerone[0, 0] && playerone[1, 1] && playerone[2, 2]) || (playerone[0, 2] && playerone[1, 1] && playerone[2, 0])) { winone = true; } } static void ifwonb() { if ((playertwo[0, 0] && playertwo[1, 0] && playertwo[2, 0]) || (playertwo[0, 1] && playertwo[1, 1] && playertwo[2, 1]) || (playertwo[0, 2] && playertwo[1, 2] && playertwo[2, 2]) || (playertwo[0, 0] && playertwo[1, 0] && playertwo[2, 0]) || (playertwo[0, 1] && playertwo[1, 1] && playertwo[2, 1]) || (playertwo[0, 2] && playertwo[1, 2] && playertwo[2, 2]) || (playertwo[0, 0] && playertwo[1, 1] && playertwo[2, 2]) || (playertwo[0, 2] && playertwo[1, 1] && playertwo[2, 0])) { wintwo = true; } } } }
thanks young programmer
when reformat code, looks of win conditions repeated.
(playerone[0, 0] && playerone[1, 0] && playerone[2, 0]) || // here (playerone[0, 1] && playerone[1, 1] && playerone[2, 1]) || (playerone[0, 2] && playerone[1, 2] && playerone[2, 2]) || (playerone[0, 0] && playerone[1, 0] && playerone[2, 0]) || // , here (playerone[0, 1] && playerone[1, 1] && playerone[2, 1]) || (playerone[0, 2] && playerone[1, 2] && playerone[2, 2]) || (playerone[0, 0] && playerone[1, 1] && playerone[2, 2]) || (playerone[0, 2] && playerone[1, 1] && playerone[2, 0]))
take other repeated win conditions well.
Comments
Post a Comment