br Posted March 14, 2012 Report Share Posted March 14, 2012 Boas pessoal estou a fazer uns teste com matrizes. quero criar um tabuleiro de xadrez, penso que estou a criar isto bem, mas aparece-me um erro (se of unassigned local variable 'tabuleiro') { int[][] matriz= new int[8][]; PictureBox[][] tabuleiro; int lx=0, ly=0; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (x % 2 != 0) matriz[x][y] = 1; matriz[x][y] = 0; } } for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { tabuleiro[x][y] = new PictureBox(); //erro só aparece aqui tabuleiro[x][y].Location = new Point(lx, ly); tabuleiro[x][y].Size = new System.Drawing.Size(50, 50); tabuleiro[x][y].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; if (matriz[x][y]==1) tabuleiro[x][y].Image = Properties.Resources.branco; else tabuleiro[x][y].Image = Properties.Resources.preto; Controls.Add(tabuleiro[x][y]); pictureBox1.Controls.Add(tabuleiro[x][y]); ly += 50; } lx += 50; } } alquem me pode ajudar? Link to comment Share on other sites More sharing options...
bsccara Posted March 14, 2012 Report Share Posted March 14, 2012 Parece-me que defines a variável 'tabuleiro' como uma matriz de PictureBox's (na segunda linha) mas não a inicializas, o que fazes na primeira linha com a matriz de inteiros. Deves ter de usar o construtor assim: PictureBox[][] tabuleiro = new PictureBox[8][8]; Link to comment Share on other sites More sharing options...
br Posted March 14, 2012 Author Report Share Posted March 14, 2012 exacto, acabei por chegar a essa conclusao tambem. fica entao o codigo assim: int[][] matriz; PictureBox[][] tabuleiro; public Form1() { InitializeComponent(); iniciar(); } public void iniciar() { matriz = new int[8][]; tabuleiro= new PictureBox[8][]; for (int i = 0; i < 8; i++) { matriz[i] = new int[8]; tabuleiro[i] = new PictureBox[8]; } int lx=0, ly=0; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (x % 2 != 0) matriz[x][y] = 1; else matriz[x][y] = 0; } } for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { tabuleiro[y][x] = new PictureBox(); tabuleiro[y][x].Location = new Point(ly, lx); tabuleiro[y][x].Size = new System.Drawing.Size(50, 50); tabuleiro[y][x].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; if (matriz[y][x] == 1) tabuleiro[y][x].Image = Properties.Resources.branco; else tabuleiro[y][x].Image = Properties.Resources.preto; Controls.Add(tabuleiro[y][x]); pictureBox1.Controls.Add(tabuleiro[y][x]); lx += 50; } ly += 50; } } ainda nao faz o que quero, mas ja desenha alguma coisa na form 😄 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now