Skip to content

Instantly share code, notes, and snippets.

@Andersoncrh
Last active July 10, 2024 15:07
Show Gist options
  • Save Andersoncrh/5759371 to your computer and use it in GitHub Desktop.
Save Andersoncrh/5759371 to your computer and use it in GitHub Desktop.
Simulador de bingo em c#, com apenas 1 jogador, informa quina, e cartela cheia!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Trabalho_Bingo
{
class Bingo
{
//constructor da classe
public Bingo(Form f)
{
tab = new Tabela(f);
car = new Cartela(f);
}
public Tabela tab;//variável tab do tipo tabela
public Cartela car;//variável car do tipo cartela
int[] sorteados = new int[60];//guarda numeros sorteados para verificação
int[] encontrados = new int[25];//guarda numeros pintados na cartela para controle
int ultnro=-1;//ultimo numero sorteado
int ultpos = -1;//ultima posicao pintada na cartela
public int[] Encontrados
{
get { return encontrados; }
set { encontrados = value; }
}
//#################METODOS DO BINGO#####################
//sorteia numero válido pinta botao da tabela se o numero existir na cartela pinta botao da cartela guarda o numero sorteado e a posicao do ultimo botao pintado na cartela
public void ProximaRodada(Form f)
{
int nro=0;
do
{
nro = car.SortearNumero();
}while (car.ValidarNumero(sorteados, nro, 60) == false);
tab.btn[nro-1].BackColor = Color.Yellow;
if (ultnro!=-1)
tab.btn[ultnro-1].BackColor = Color.CornflowerBlue;
sorteados[nro-1] = nro;
int posicao = car.MarcarNumero(nro, f);
Encontrados[car.pintados] = posicao;
if (ultpos!=-1)
if (car.Quin !=-1)
car.Btn[ultpos].BackColor = Color.CornflowerBlue;
ultnro = nro;
ultpos = posicao;
}
//zerar vetor que guarda numeros sorteados
public void LimparSorteados(Form f)
{
ultnro = -1;
ultpos = -1;
for (int j = 0; j < 60; j++)
{
sorteados[j] = 0;
}
}
//pintar botoes randomicamente quando der bingo
public void Bingow(Form f,Button b)
{
int n;
Random rdn = new Random();
for (int i = 1; i < 60; i++)
{
n = rdn.Next(0, 9);
if (n == 0)
tab.btn[i].BackColor = Color.CornflowerBlue;
if (n == 1)
tab.btn[i].BackColor = Color.Green;
if (n == 2)
tab.btn[i].BackColor = Color.Red;
if (n == 3)
tab.btn[i].BackColor = Color.Yellow;
if (n == 4)
tab.btn[i].BackColor = Color.LightBlue;
if (n == 5)
tab.btn[i].BackColor = Color.Blue;
if (n == 6)
tab.btn[i].BackColor = Color.Azure;
if (n == 7)
tab.btn[i].BackColor = Color.Brown;
if (n == 8)
tab.btn[i].BackColor = Color.White;
}
for (int i = 0; i < 25; i++)
{
n = rdn.Next(0, 9);
if (n == 0)
car.Btn[i].BackColor = Color.CornflowerBlue;
if (n == 1)
car.Btn[i].BackColor = Color.Green;
if (n == 2)
car.Btn[i].BackColor = Color.Red;
if (n == 3)
car.Btn[i].BackColor = Color.Yellow;
if (n == 4)
car.Btn[i].BackColor = Color.LightBlue;
if (n == 5)
car.Btn[i].BackColor = Color.Blue;
if (n == 6)
car.Btn[i].BackColor = Color.Azure;
if (n == 7)
car.Btn[i].BackColor = Color.Brown;
if (n == 8)
car.Btn[i].BackColor = Color.White;
}
}
//############################################################
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Trabalho_Bingo
{
class Cartela
{
//constructor da classe
public Cartela(Form f)
{
CriarCartela(f);
}
Button[] btn = new Button[25];//vetor de botoes da cartela
int x = 680, y = 50;//posicionamento inicial da cartela
static int contcar = 0;//contador utilizado para criar botoes da cartela
int[] cartela = new int[25];//ao criar a catela os números apresentados no btn[].Text são guardados neste vetor
public int Quin;//contador para verificar quina, enquanto for diferente de -1ou-2 chama os metodos de quina, quando for igual a cinco anuncia quina e recebe -1
public int pintados = 0;//contador para verificar a quantidade de numeros pintados na cartela, sempre que for igual a 24 é anunciado o fim do jogo
//################Variáveis Refatoradas######################
public Button[] Btn
{
get { return btn; }
set { btn = value; }
}
public int[] Cartela1
{
get { return cartela; }
set { cartela = value; }
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public int Contcar
{
get { return contcar; }
set { contcar = value; }
}
//###################################################
//#################METODOS DA CARTELA#####################
//Instancia o vetor de botoes btn criando 25 botoes e mostrando apenas 24(1 coringa)
public void CriarCartela(Form f)
{
for (int j = 0; j < 5; j++)
{
for (int l = 0; l < 5; l++)
{
int nro;
if (Contcar == 12)
{
X = X + 45;
PictureBox pic = new PictureBox();
pic.Image = Properties.Resources.coringa;
Btn[Contcar] = new Button();
Btn[Contcar].BackColor = Color.CornflowerBlue;
pic.Size = new System.Drawing.Size(50, 50);
pic.Location = new System.Drawing.Point(765, 135);
Btn[Contcar].Enabled = false;
f.Controls.Add(pic);
}
else
{
do
{
nro = SortearNumero();
} while (ValidarNumero(Cartela1, nro, 24) == false);
Cartela1[Contcar] = nro;
Btn[Contcar] = new Button();
Btn[Contcar].Size = new System.Drawing.Size(40, 40);
Btn[Contcar].Name = Contcar.ToString();
Btn[Contcar].Text = nro.ToString();
Btn[Contcar].BackColor = Color.LightGray;
Btn[Contcar].Location = new System.Drawing.Point(X, Y);
Btn[Contcar].Enabled = false;
f.Controls.Add(Btn[Contcar]);
X = X + 45;
}
Contcar++;
}
Y = Y + 45;
X = 680;
}
}
//Sorteia numero Randomico
public int SortearNumero()
{
Random rdn = new Random();
int nro;
nro = rdn.Next(1, 61);
return (nro);
}
//Verifica se numero já foi sorteado
public bool ValidarNumero(int[] vetor, int nro, int qnt)
{
for (int i = 0; i < qnt; i++)
{
if (nro == vetor[i])
{
return false;
}
}
return true;
}
//marca numero sorteado e validado na cartela
public int MarcarNumero(int nro,Form f)
{
for (int i = 0; i < 25; i++)
{
if (nro == Cartela1[i])
{
pintados++;
Btn[i].BackColor = Color.Yellow;
return i;
}
}
return -1;
}
//reinicia cartela com as caracteristicas padrões
public void ReiniciarCartela(Form f)
{
X = 680; Y = 50;
int contador = 0;
int teste = 0;
Btn[12].BackColor = Color.CornflowerBlue;
for (int j = 0; j < 5; j++)
{
for (int l = 0; l < 5; l++)
{
contador++;
//if (contador == 25)
// contador--;
int nro;
if (contador == teste + 13)
{
X = X + 45;
teste += 13;
}
else
{
do
{
nro = SortearNumero();
} while (ValidarNumero(Cartela1, nro, 24) == false);
Cartela1[contador - 1] = nro;
Btn[contador - 1].Size = new System.Drawing.Size(40, 40);
Btn[contador - 1].Name = Contcar.ToString();
Btn[contador - 1].Text = nro.ToString();
Btn[contador - 1].BackColor = Color.LightGray;
Btn[contador - 1].Location = new System.Drawing.Point(X, Y);
Btn[contador - 1].Enabled = false;
f.Controls.Add(Btn[contador - 1]);
X = X + 45;
}
}
Y = Y + 45;
X = 680;
}
}
//###########################################################################
//#################METODOS DE VERIFICACAO DE QUINA#####################
public bool QuinaHori(Form f)
{
for (int j = 0; j < 5; j++)
{
Quin = 0;
int[] colorir = new int[5];
for (int i = 0; i < 5; i++)
if (Btn[i + (j * 5)].BackColor != Color.LightGray)
{
colorir[i] = i + (j * 5);
Quin++;
}
if (Quin == 5)
{
for (int i = 0; i < 5; i++)
{
Btn[colorir[i]].BackColor = Color.Green;
}
return true;
}
}
return false;
}
public bool QuinaVert(Form f)
{
for (int j = 0; j < 5; j++)
{
int[] colorir = new int[5];
Quin = 0;
for (int i = 0; i < 5; i++)
if (Btn[j + (i * 5)].BackColor != Color.LightGray)
{
colorir[i] = j + (i * 5);
Quin++;
}
if (Quin == 5)
{
for (int i = 0; i < 5; i++)
{
Btn[colorir[i]].BackColor = Color.Green;
}
return true;
}
}
return false;
}
public bool QuinaTrans1(Form f)
{
if ((Btn[0].BackColor != Color.LightGray) && (Btn[6].BackColor != Color.LightGray) && (Btn[18].BackColor != Color.LightGray) && (Btn[24].BackColor != Color.LightGray))
{
Btn[0].BackColor = Color.Green;
Btn[6].BackColor = Color.Green;
Btn[18].BackColor = Color.Green;
Btn[24].BackColor = Color.Green;
return true;
}
return false;
}
public bool QuinaTrans2(Form f)
{
if ((Btn[4].BackColor != Color.LightGray) && (Btn[8].BackColor != Color.LightGray) && (Btn[16].BackColor != Color.LightGray) && (Btn[20].BackColor != Color.LightGray))
{
Btn[4].BackColor = Color.Green;
Btn[8].BackColor = Color.Green;
Btn[16].BackColor = Color.Green;
Btn[20].BackColor = Color.Green;
return true;
}
return false;
}
//###########################################################################
public bool CartelaCheia(Form f, Button botao)
{
if (pintados == 24)
{
botao.Enabled = false;
for (int i = 0; i < 25; i++)
{
btn[i].BackColor = Color.Red;
}
MessageBox.Show(" Bingo!!!\n Você completou a cartela!!\n Clique em Novo Jogo(F5) para jogar novamente!!\n ou em Sair(Ctrl+del) para fexar o programa!!");
return (true);
}
return (false);
}
//###########################################################################
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Trabalho_Bingo
{
public partial class Form1 : Form
{
int cont = 0;//contador utilizado no label6 para mostrar quantidade de rodadas
Bingo bin;//variável bin do tipo bingo
Timer bingow = new Timer();//timer utilizado para pitnar botoes randomicamente quando der bingo
public Form1()
{
InitializeComponent();
bin = new Bingo(this);//stancia um novo bingo
bingow.Interval = 1000;//intervalo do timer bingow
bingow.Tick += new EventHandler(bingow_Tick);//tick do timer bingow
}
private void bingow_Tick(object sender, EventArgs e)
{
bin.Bingow(this, button2);//tick do timer chama o metodo bingow quando o jogo acaba
}
private void Form1_Load(object sender, EventArgs e)
{
}
//botao proxima rodada, chama metodo ProximaRodada da classe Bingo, enquanto não der quina chama as funções de quina da classe Cartela, se a cartela estiver cheia chama o metodo CartelaCheia da classe cartela e ativa o timer bingow
private void button2_Click(object sender, EventArgs e)
{
cont++;
label6.Text = cont + "ª RODADA".ToString();
bin.ProximaRodada(this);
if (bin.car.Quin == -1)
bin.car.Quin = -2;
if (bin.car.Quin != -1 && bin.car.Quin != -2)
{
if (((bin.car.QuinaTrans1(this) || bin.car.QuinaTrans2(this) || (bin.car.QuinaVert(this)) || (bin.car.QuinaHori(this)) == true )))
{
MessageBox.Show("Quina encontrada");
bin.car.Quin=-1;
}
}
if (bin.car.CartelaCheia(this, button2) == true)
{
bingow.Enabled = true;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void novoJogoToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sairToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
//mostra informações dos desenvolvedores
private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(" Desenvolvido por:\n Anderson Hipocreme RA: 1418/12\n Leandro Zatin RA: 0731/12");
}
//mostra ajuda
private void ajudaToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Legenda:\n Cor Amarela = Ultimo Número Sorteado\n Cor Azul = Números já Sorteados \n Cor Verde = Quina Encontrada \n Cor Vermelha = Cartela Cheia\n\nTeclas de Atalho: \n F1 = Ajuda \n F2 = Sobre \n F5 = Novo Jogo \n Ctrl+Del = Sair");
}
//reinicia o jogo
private void novoJogoToolStripMenuItem1_Click(object sender, EventArgs e)
{
bingow.Enabled = false;
bin.LimparSorteados(this);
bin.car.ReiniciarCartela(this);
bin.car.pintados = 0;
bin.tab.ReiniciarTabela(this);
button2.Enabled = true;
bin.car.Quin = 0;
cont = 0;
label6.Text = " ";
}
//fexa o jogo
private void sairToolStripMenuItem1_Click(object sender, EventArgs e)
{
Close();
}
}
}
namespace Trabalho_Bingo
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.novoJogoToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.sobreToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ajudaToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.sairToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// button2
//
this.button2.BackColor = System.Drawing.Color.LightGray;
this.button2.Location = new System.Drawing.Point(518, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(100, 80);
this.button2.TabIndex = 0;
this.button2.Text = "Sortear";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(483, 44);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(183, 55);
this.label1.TabIndex = 0;
this.label1.Text = "BINGO";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label6.Location = new System.Drawing.Point(530, 126);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(0, 13);
this.label6.TabIndex = 7;
this.label6.Click += new System.EventHandler(this.label6_Click);
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.novoJogoToolStripMenuItem1,
this.sobreToolStripMenuItem,
this.ajudaToolStripMenuItem,
this.sairToolStripMenuItem1});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(949, 24);
this.menuStrip1.TabIndex = 8;
this.menuStrip1.Text = "menuStrip1";
//
// novoJogoToolStripMenuItem1
//
this.novoJogoToolStripMenuItem1.Name = "novoJogoToolStripMenuItem1";
this.novoJogoToolStripMenuItem1.ShortcutKeys = System.Windows.Forms.Keys.F5;
this.novoJogoToolStripMenuItem1.Size = new System.Drawing.Size(76, 20);
this.novoJogoToolStripMenuItem1.Text = "Novo Jogo";
this.novoJogoToolStripMenuItem1.Click += new System.EventHandler(this.novoJogoToolStripMenuItem1_Click);
//
// sobreToolStripMenuItem
//
this.sobreToolStripMenuItem.Name = "sobreToolStripMenuItem";
this.sobreToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F2;
this.sobreToolStripMenuItem.Size = new System.Drawing.Size(49, 20);
this.sobreToolStripMenuItem.Text = "Sobre";
this.sobreToolStripMenuItem.Click += new System.EventHandler(this.sobreToolStripMenuItem_Click);
//
// ajudaToolStripMenuItem
//
this.ajudaToolStripMenuItem.Name = "ajudaToolStripMenuItem";
this.ajudaToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F1;
this.ajudaToolStripMenuItem.Size = new System.Drawing.Size(50, 20);
this.ajudaToolStripMenuItem.Text = "Ajuda";
this.ajudaToolStripMenuItem.Click += new System.EventHandler(this.ajudaToolStripMenuItem_Click);
//
// sairToolStripMenuItem1
//
this.sairToolStripMenuItem1.Name = "sairToolStripMenuItem1";
this.sairToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Delete)));
this.sairToolStripMenuItem1.Size = new System.Drawing.Size(38, 20);
this.sairToolStripMenuItem1.Text = "Sair";
this.sairToolStripMenuItem1.Click += new System.EventHandler(this.sairToolStripMenuItem1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Gainsboro;
this.ClientSize = new System.Drawing.Size(949, 377);
this.Controls.Add(this.label6);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Trabalho Bingo";
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem sobreToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ajudaToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem novoJogoToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem sairToolStripMenuItem1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Trabalho_Bingo
{
class Tabela
{
public Tabela(Form f)
{
CriarTabela(f);
}
public Button[] btn = new Button[60];//vetor de botoes da classe
int x = 22, y = 50;//posicionamento inicial da tabela
int contab = 0;//contador da classe
//################Variáveis Refatoradas######################
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public int Contab
{
get { return contab; }
set { contab = value; }
}
//############################################################
//#################METODOS DA TABELA#####################
//Instancia o vetor de botoes btn criando 60 botoes
private void CriarTabela(Form f)
{
for (int j = 0; j < 6; j++)
{
for (int l = 0; l < 10; l++)
{
btn[Contab] = new Button();
btn[Contab].Size = new System.Drawing.Size(40, 40);
btn[Contab].Text = (Contab+1).ToString();
btn[Contab].Name = Contab.ToString();
btn[Contab].BackColor = Color.LightGray;
btn[Contab].Location = new System.Drawing.Point(X, Y);
btn[Contab].Enabled = false;
f.Controls.Add(btn[Contab]);
X = X + 45;
Contab++;
}
Y = Y + 45;
X = 22;
}
}
//reinicia tabela com as caracteristicas padrões
public void ReiniciarTabela(Form f)
{
int contador = 0;
Y = 50;
X = 22;
for (int j = 0; j < 6; j++)
{
for (int l = 0; l < 10; l++)
{
btn[contador].BackColor = Color.LightGray;
X = X + 45;
contador++;
}
Y = Y + 45;
X = 22;
}
}
//###########################################################################
}
}
@champagneflacko
Copy link

me gusta el codigo
puedes pasarme la solucion por favor

para seguir trabajando

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment