jeudi 28 septembre 2017

C# Check if win function for TicTacToe

I am trying to create an function to check if there is an win (horizontally). So I thought this out on paper but could not get it to fully work. My code has 2 flaws at this point.

  1. It only checks first row. and does not work after that.
  2. Whatever is in the first row. lets say in point 0,1[X] 0,2[O] 0,3[X] it wil retrun that there is an "true"

Here is the code.

//Public Var
int n = 0;

//How I test if function is True and when. (every time an O or X is placed i do this:)

if (checkwinnner() == true)
{
    MessageBox.Show("Someone won.");
}

//Function
public bool checkwinnner() 
{  
    bool bw = true;
    for (int r = 0; r < n; r++)
    {
        bw = true;
        if (bar[r, 0].Text != "")
        {
            for (int c = 1; c < n; c++)
            {
                if (bar[r, 0].Text != bar[r, c].Text)
                {
                    bw = false; break;
                }
            }
            bw = true;
        }
        else 
        { 
            bw = false; 
        }
    }
    return bw;
}

So This is all there is to this function as of yet. Im buisy with it atm. So. I used an bool to check if someone won. true is win false is no win yet.

  • N = height and with of field. its 3x3 always but can be changed trought textbox.
  • R = row
  • C = column

so I first put in an for loop to loop every row. Then I check if text is not empty. becouse if its empty it cant be 3 in a row in a 3x3 field horizontally. After that i need to do a for loop for every column. and check if text in columns 1 is equal to 2 and 3.

However I stated my buggs atm at the top of the post and would like to ask:

Any tips on what I can fix or am doing wrong. I would like to use this code and not some if statement that checks the buttons like if((0,1 && 0,2 && 0,3) == X || Y) or something like that. becouse the field can be 4x4 and 5x5 to.

Thank you in advance. And I hope my question is formatted correcly.

Happy coding.

If Statement with multiple OR Values

I'm currently working on a Simple Riddles Game outside of class, and what I want to do is have a statement that will verify what the question is, and if the answer that was put in is correct. Here is the Code I have so far:

private void butt_Submit_Click(object sender, EventArgs e)
        {
            if (lab_Riddle.Text == "What Walks on 4 Legs in the Morning, 2 in the Afternoon and 3 in the Evening?")

                if (TeBo_Ans.Text == "A Man") ;
;             {
                MessageBox.Show("Well Done");
                lab_Riddle.Text = ("I have an Eye but Cannot See- What Am I?");
             }
            if (TeBo_Ans.Text == "Mankind") ;
             {
                MessageBox.Show("Good Words");
                lab_Riddle.Text = ("I have an Eye but Cannot See- What Am I?");
             }
            if (TeBo_Ans.Text == "People") ;
             {
                MessageBox.Show("Yes");
                lab_Riddle.Text = ("I have an Eye but Cannot See- What Am I?");
             }
            if (TeBo_Ans.Text == "A Person") ;
             {
                MessageBox.Show("Exactly");
                lab_Riddle.Text = ("I have an Eye but Cannot See- What Am I?");
             }
            if (TeBo_Ans.Text == "Humankind") ;
             {
                MessageBox.Show("Exactly");
                lab_Riddle.Text = ("I have an Eye but Cannot See- What Am I?");
             }
        } 

Here, lab_Riddle is the label containing the Question, and TeBo_Ans is the textbox the user types the answer into. This code sort of Works, but ideally I'd like to condense it down to something like

If lab_Riddle.Text= [Riddle 1]
 {
  If TeBo_Ans == [a] OR [b] OR [d] 
   {
    MessageBox.Show ("Correct")
    lab_Riddle.Text [riddle 2]
    }
   Else 
   { 
    MessageBox.Show ("Incorrect")
  }
}

Language is C# if I haven't already said., and vertical lines. This is probably a really long and convoluted way of doing this, but i'm still very new to coding.

Thanks for your help!

if-else, ternary operator readability

I've been discussing with my colleges and I want your opinion , which option is better in terms of readability?

private boolean isGreatherThan(int a, int b){

    return a>b ? true : false;
}

private boolean isGreatherThan(int a, int b){

    if(a>b){

        return true;
    }   

    return false;
} 


 private boolean isGreatherThan(int a, int b){

    if(a>b){   

            return true;

        }else{  

            return false;
        }
    }

Check Quiz Answer with color code

I got my little simple Quiz up and running but there is one more feature I would like to implement, color coding for correct and wrong answers. I have 4 Buttons that randomly display there answers. I would like to implement a highlighted button of the correct answer after I pressed the wrong answer that changes the button color to red. my code of this part so far:

// Check Answer.
public void checkAnswer(View view) {

    // Get clicked button.
    final Button answerBtn = (Button) findViewById(view.getId());
    String btnText = answerBtn.getText().toString();

    if (btnText.equals(rightAnswer)) {
        rightAnswerCount++;
        Toast.makeText(getApplicationContext(), "Correct", 
        Toast.LENGTH_SHORT).show();
        soundPlayer.playCorrectSound();
        answerBtn.setBackgroundColor(Color.parseColor("#3fd250"));

    } else {

        soundPlayer.playWrongSound(); // Wrong Sound.
        Toast.makeText(getApplicationContext(), "Wrong " + rightAnswer, 
        Toast.LENGTH_SHORT).show();
        answerBtn.setBackgroundColor(Color.parseColor("#da251c")); 
        quizLife--;

    }
}

So in the else part I would like to color code my correct answer to green. I get my questions and answers from a sqlite Database.

Getting errors with user console input

I attempted to make a basic menu for the user to select the option they want, but receive an error when the user inputs an answer. Is there any way to make this work/more efficient and still keep it basic?

 Console.WriteLine("Enter '1' to multiply");
        Console.WriteLine("Enter '2' to divide");
        Console.ReadLine();

        int answer;
        answer = Convert.ToInt16(Console.ReadLine());   //get an error in this line

        if (answer == 1)
        {
            int number1;
            int number2;

            Console.WriteLine("You chose to multiply");
            Console.ReadLine();

            Console.Clear();

            Console.WriteLine("Enter a number..");
            number1 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Enter another number..");
            number2 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();

            Console.WriteLine("Your answer is --> " + number1 * number2);
        }

        else if (answer != 1)
        {
            int number3;
            int number4;

            Console.WriteLine("You chose to divide");
            Console.ReadLine();

            Console.Clear();

            Console.WriteLine("Enter a number..");
            number3 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Enter another number..");
            number4 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();

            Console.WriteLine("Your answer is --> " + number3 / number4);
        }

C++ grouping rules with FOR and IF statements

Just got burned by a strange grouping issue involving a for loop and if statement. I wrote:

for (int i = 0; i < baseMtx->valueSize; i++)
    if (!tempMtx->copyValue(baseMtx, i, i, scaleMult, scaleImag))
        throw ErrorClass("Matrix expansion failed while copying element values");

This throws the error after exiting the loop, and I have not been able figure out why. The way I understand grouping rules in C++, this should be equivalent to

for (int i = 0; i < baseMtx->valueSize; i++)
{
    if (!tempMtx->copyValue(baseMtx, i, i, scaleMult, scaleImag))
        throw ErrorClass("Matrix expansion failed while copying element values");
}

--- but it isn't. For future reference and as a learning experience, can someone explain why the brackets are necessary?

React-native if condition in List Item

I have List that render an array how can i put an if condition statement ?.. for example

if (item.ticketId === 2) { and in the if statement the data render will be in different color }

do I need to create another function ?

           <Content>
                <ScrollView>
                <List>
                  { this.state.ticket.map((item, i) => (
                    <ListItem
                    roundAvatar
                    key={i}
                    avatar={
                      <View >
                        <Text>{item.ticketId}</Text>
                      </View>
                    }
                      title={
                        <View>
                          <Text>ROW :{item.row}</Text>
                        </View>
                      }
                      subtitle={
                        <View>
                          <Text>GATE :{item.gate}</Text>
                        </View>
                      }
                    />
                  ))
                  }
                  </List>
                </ScrollView>
              </Content>