using System; using System.Drawing; using System.Text; using System.Windows.Forms; public interface LayoutableInterface { void updateLayout(); } public class Form1 : Form, LayoutableInterface { //this is a hack to deal with the fact, that the taskbar is counted in the window size //Vous n'avez pas besoin de faire pareil! //restez avec la resolution indiqué dans le TP3 private int borderSizeX = -1; private int borderSizeY = -1; private Button closeButton; private TicTacToe ticTacToe; public Form1() { InitializeComponent(); this.Resize += new EventHandler(LayoutHandling); closeButton.Click += new EventHandler(closeApplication); } private void closeApplication(object sender, EventArgs e) { this.Close(); } public void updateLayout() { //calculate the borders of a window if (borderSizeX < 0) { this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; int wNB = this.Size.Width; int hNB = this.Size.Height; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; int wB = this.Size.Width; int hB = this.Size.Height; borderSizeX = wB - wNB; borderSizeY = hB - hNB; } int realWidth = this.Size.Width - borderSizeX; int realHeight = this.Size.Height - borderSizeY; this.ticTacToe.Location = new System.Drawing.Point(0, 0); this.ticTacToe.Size = new System.Drawing.Size(realWidth, realHeight - realHeight / 20); closeButton.Location = new System.Drawing.Point(0, realHeight - realHeight / 20); closeButton.Size = new Size(realWidth, realHeight / 20); ticTacToe.updateLayout(); } private void InitializeComponent() { this.ticTacToe = new TicTacToe(); this.SuspendLayout(); // // TicTacToe // this.ticTacToe.Name = "TicTacToe"; this.ticTacToe.Location = new System.Drawing.Point(0, 0); this.ticTacToe.Size = new System.Drawing.Size(200, 200); this.ticTacToe.TabIndex = 0; this.Controls.Add(this.ticTacToe); this.Name = "UltimateTicTacToe"; this.Text = "Ultimate TicTacToe"; closeButton = new Button(); closeButton.Name = "Close"; closeButton.Text = "Close"; this.Controls.Add(closeButton); this.ResumeLayout(false); } public static void LayoutHandling(object sender, EventArgs e) { try { Console.WriteLine(sender.ToString()); LayoutableInterface li = (LayoutableInterface)sender; li.updateLayout(); } catch (InvalidCastException) { } } static void Main() { Application.EnableVisualStyles(); Form1 form1 = new Form1(); Application.Run(form1); } } //On n'a pas vu USERCONTROLS dans le cours, du coup vous pouvez directement mettre tous les buttons dans la classe au-dessus! //Ici je l'ai fait pour pouvoir passer le changement de taille en paramètre... public class TicTacToe : UserControl, LayoutableInterface { private Button[] buttons = new Button[9]; private enum GameValue { E, X, O }; private enum Winner { NONE, X, O }; GameValue[] table = new GameValue[9]; uint nbPlays = 0; int currentPlayer = 0; Winner state = Winner.NONE; bool finished = false; public TicTacToe() { InitializeComponent(); reset(); } public void reset() { for (int i = 0; i < table.Length; ++i) table[i] = GameValue.E; currentPlayer = 0; nbPlays = 0; state = Winner.NONE; updateLayout(); finished = false; } void button_pressed(object sender, EventArgs args) { if (finished) { reset(); return; } try { Button b = (Button)sender; Console.WriteLine("{0}", b.Name); finished = play(Convert.ToInt32(b.Name)); updateLayout(); } catch (InvalidCastException) { Console.WriteLine("Major problem!"); } catch (FormatException) { Console.WriteLine("Major problem 2!"); } } public void updateLayout() { int w = this.Size.Width; int h = this.Size.Height; int posX = this.Location.X; int posY = this.Location.Y; int buttonSizeX = w / 3; int buttonSizeY = h / 3; this.SuspendLayout(); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { buttons[i + 3 * j].Location = new Point(posX + i * buttonSizeX, posY + j * buttonSizeY); buttons[i + 3 * j].Size = new Size(buttonSizeX, buttonSizeY); switch (table[i + 3 * j]) { case GameValue.E: buttons[i + 3 * j].Text = ""; break; case GameValue.X: buttons[i + 3 * j].Text = "X"; break; case GameValue.O: buttons[i + 3 * j].Text = "O"; break; } this.Controls.Add(buttons[i + 3 * j]); } this.ResumeLayout(false); } private void InitializeComponent() { this.SuspendLayout(); for (int i = 0; i < buttons.Length; ++i) { buttons[i] = new Button(); buttons[i].Location = new Point(0, 0); buttons[i].Size = new Size(100, 100); buttons[i].Text = ""; buttons[i].Click += new EventHandler(button_pressed); buttons[i].Name = "" + i; this.Controls.Add(buttons[i]); } this.ResumeLayout(false); } private Winner checkWinner() { //horizontal for (int j = 0; j < 3; ++j) if (table[3 * j] != GameValue.E && table[3 * j] == table[3 * j + 1] && table[3 * j + 1] == table[3 * j + 2]) { if (table[3 * j] == GameValue.X) return Winner.X; else return Winner.O; } //vertical for (int i = 0; i < 3; ++i) if (table[i] != GameValue.E && table[i] == table[i + 3] && table[i + 3] == table[i + 6]) { if (table[i] == GameValue.X) return Winner.X; else return Winner.O; } //diagonal if (table[0] != GameValue.E && table[0] == table[4] && table[4] == table[8]) { if (table[0] == GameValue.X) return Winner.X; else return Winner.O; } if (table[2] != GameValue.E && table[2] == table[4] && table[4] == table[6]) { if (table[2] == GameValue.X) return Winner.X; else return Winner.O; } return Winner.NONE; } public void showTable() { Console.WriteLine("---------------------"); for (int j = 0; j < 3; ++j) { Console.Write("I"); for (int i = 0; i < 3; ++i) { Console.Write("{0} I ", table[i + 3 * j].ToString()); } Console.WriteLine(""); Console.WriteLine("---------------------"); } } bool play(int index) { try { GameValue curr = table[index]; if (curr != GameValue.E) { Console.WriteLine("This is already taken"); return false; } if (currentPlayer == 0) { table[index] = GameValue.O; } else { table[index] = GameValue.X; } if ((state = checkWinner()) != Winner.NONE) { Console.WriteLine("Somebody won: {0}", state); return true; } currentPlayer++; if (currentPlayer > 1) currentPlayer = 0; ++nbPlays; if (nbPlays == 9) { Console.WriteLine("Draw game"); return true; } } catch (IndexOutOfRangeException e) { Console.WriteLine("The number should have between 1 and 9"); } return false; } }