将值与数组元素进行比较

时间:2018-03-05 12:46:45

标签: c++ arrays visual-c++

当我收到用户输入cin >> x;cin >> y;

我想将其与数组“位置线”L1[i][j]进行比较,但是当我使用if语句时 - if (x && y == L1[i][j])
- if (x == L1[i] && y == L1[j])

我没有得到“HIT”的结果,这是我需要的。 这是一种扫雷项目。任何帮助或指针都将非常感激!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

char L1[8][8], L2[16][16], L3[24][24]; // grid location of cords
char B1[8][8], B2[16][16], B3[24][24]; // grid location of bombs
int i, j;

using namespace std;

// load and print grids L1 L2 L3
void loadgrid_L1()
{
    for ( i = 0; i < 8; i++)
    {
        for ( j = 0; j < 8; j++)
        {
            L1[i][j] = 'X';
        }
    }
}
void printgrid_L1() 
{
    for ( i = 0; i < 8; i++)
    {
        for ( j = 0; j < 8; j++)
        {
            cout << L1[i][j];
        }
        cout << ("\n");
    }
}

void loadgrid_L2()
{
    for (i = 0; i < 16; i++)
    {
        for (j = 0; j < 16; j++)
        {
            L2[i][j] = 'X';
        }
    }
}
void printgrid_L2()
{
    for (i = 0; i < 16; i++)
    {
        for (j = 0; j < 16; j++)
        {
            cout << L2[i][j];
        }
        cout << ("\n");
    }
}

void loadgrid_L3()
{
    for (i = 0; i < 24; i++)
    {
        for (j = 0; j < 24; j++)
        {
            L3[i][j] = 'X';
        }
    }
}
void printgrid_L3()
{
    for (i = 0; i < 24; i++)
    {
        for (j = 0; j < 24; j++)
        {
            cout << L3[i][j];
        }
        cout << ("\n");
    }
}

void bomb1() 
{
    L1[1][5] = 'O';
}

void bomb2()
{
    L2[3][4] = 'O';
}

void bomb3()
{
    L3[6][6] = 'O';
}

void menu() //need to work on this section
{
    string lvl, x, y;

    cout << "Please select a lvl to play L1, L2, L3 " << endl;
    getline(cin, lvl);

    if (lvl == "L1")
    {
        loadgrid_L1();
        //moved bomb() from here

        cout << "Please enter your co-ordinates for your move" << endl;
        cout << "Cord 1: "; 
        getline(cin, x);
        cout << "Cord 2: ";
        getline(cin, y);

        if (x == L1[i])
        {
            cout << "HIT!" << endl;
        }
        else if (x != L1[i])
        {
            cout << "SUCCESS!" << endl;
        }

        bomb1();
        printgrid_L1();


    }
    else if (lvl == "L2")
    {
        loadgrid_L2();
        bomb2();
        printgrid_L2();
    }
    else if (lvl == "L3")
    {
        loadgrid_L3();
        bomb3();
        printgrid_L3();
    }
    else if ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
    {
        cout << "You must pick a lvl" << endl;
        while ((lvl != "L1") && (lvl != "L2") && (lvl != "L3"))
        {
            getline(cin, lvl);
            if (lvl == "L1")
            {
                loadgrid_L1();
                bomb1();
                printgrid_L1();
            }
            else if (lvl == "L2")
            {
                loadgrid_L2();
                bomb2();
                printgrid_L2();
            }
            else if (lvl == "L3")
            {
                loadgrid_L3();
                bomb3();
                printgrid_L3(); // needs to stop
            }
        }
    }
}

int main()
{   
    menu();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您可以轻松更改每个loadgrid&amp; printgrid函数是2个单独的函数:

<?php
define('APP_DIR', 'app');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
define('WEBROOT_DIR', 'webroot');
define('WWW_ROOT', ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS);

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');
}

require APP_DIR . DS . WEBROOT_DIR . DS . 'index.php';

?>

您可以按照上面相同的模式进行打印网格。

然后当您调用该函数时,您只需传递要加载和打印的文件。