Naval battle program c#

Asked

Viewed 940 times

-1

I have to make a naval battle game for college using windows Forms in visual studio, the problem is that I could not understand the logic to generate naval battle maps, I can not think of a way to do this.

What must be done is to put custom boats, in addition to the ones that already have, which are: One aircraft carrier (five squares each), Two tanks (four squares each), Three torpedo boats (three squares each), Four submarines (two squares each).

The player also enters with the size of the map, with this is generated 2 maps, one for player 1 and one for player 2. When player 1 enters with all ships, they hide and there are disabled, so player 2 can put their ships and they hide and then swap the maps to start the game.

Told me to use picturebox[,] but how does it work? Someone has done something like this before?

I have to save the ranking on file but I have an idea how to do that. I made a code here for menu.

Player class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BatalhaNaval
{
    class Jogador
    {
        private string nome;
        private int pontos;
        public Jogador()
        {

        }
        public int Pontos
        {
            get
            {
                return pontos;
            }
            set
            {
                pontos = value;
            }
        }
        public string Nome
        {
            get
            {
                return nome;
            }
            set
            {
                nome = value;
            }
        }
    }
}

Menu Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BatalhaNaval
{
    public partial class menu : Form
    {
        public menu()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Formulario janelanome = new Formulario();
            janelanome.Show();
            Hide();
        }
    }
}

Player name and map size form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BatalhaNaval
{
    public partial class Formulario : Form
    {
        public Formulario()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            jogo janelajogo = new BatalhaNaval.jogo();
            janelajogo.Show();
            Hide();
        }
    }
}

Menu print:

Menu

Form print:

Formulario

  • I managed to make a picture box custom but as I will work with him to store the ships, and as I will put the ships in the 2 trays?

  • You’ve already made progress on your project?

  • 3

    Life tip: separate game logic from your screen representation. Make the whole game without thinking about screen, then you think about which screen components will use.

  • i am doing by picture box the problem is that are 7x7 to 15x15 the size of the board that can be chosen, and to put the ships is being difficult because I am doing if by if I do not know if there is a more efficient way to do this

1 answer

1

So you can do it in different ways... There may be easier way, but I would do it in a Datagridview, using Rows and Columns to make the map.

To fill the map with the proper ships, you can create buttons that define how many squares you can occupy. For example, when you click the TORPEDO BOAT button, you define that only 3 clicks can be done (through a counter).

You should also check if the Datagridview cell has already been filled, to check the players' hits (to sink the ships or count how many more to be). It will also be necessary to link the respective cells to each added ship, to warn if it has been hit or has already been sunk, for example:

COLUNA/LINHA       NAVIO 
     A2         DESTROIER_1
     A3         DESTROIER_1
     A4         DESTROIER_1
     C4         DESTROIER_2
     D4         DESTROIER_2
     E4         DESTROIER_2

Again, there are N ways you can do this.

edit1: answering @Arthur’s question

inserir a descrição da imagem aqui

If you clicked here (1), you have to check if the clicks can be on the red items,and for that, you can use mathematics as an ally to perform the checks. Again, maybe that’s not the best way to do this, but it’s an approach I would probably take.

  • using counter as I could treat an error if he put diagonally the ship or put in place other than allowed?

  • Well, if you think about it, it’s a little boring to do. Type, first you have to set what is forbidden, if you clicked on a point, you have to check if the next point clicked is according to the requirements. Look at the edited.

  • 1

    I’m thinking of a way here thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.