Sometimes endless loop after connected the DC motor

If I disconnect the DC motor, the program running is ok but if connected the DC motor, I push the button OK ,or number on RCU, the program will go to endless loop(the motor won't stop) sometimes. Can anyone help me to look into it and check what is the real problem, very appreciate that. Below is my key code.

/* This is the auto playing cards dealer machine made by William Shi*/

/*-----( Import needed libraries )-----*/
#include "IRremote.h"
#include "QMC5883.h"
#include "Wire.h"
#define DEBUGLEVEL 1
#define SPEED1 130
#define SPEED2 130
/*-----( Declare Constants )-----*/
int iPlayers = 4;           //set 4 default players
int iCardEach = 13;         //set 13 playing cards for each one as default
int iPlayerDistance = 90;   //set 90 degrees for players distance
unsigned long iDealCards = 4000;      //dealing one card with 2 seconds
//unsigned long iIntervalofgotoplayer = 4000;
bool isSetPlayer = false;   //will set players number if true
bool isSetCardForEach = false;//will set card for each player if true
bool isDoneSetCardForEach = false;//completed set card for each player if true
/*-----( Declare objects )-----*/
IRrecv irrecv(12);         // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

/*-----( Declare Variables )-----*/
//int 6 = 6;       // pin 6 for get signal from the infrared obstacle avoidance module
QMC5883 compass;
//int dx = 0;
//int dy = 0;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  if (DEBUGLEVEL) {
    Serial.begin(9600);
    Serial.println("YourDuino.com IR Infrared Remote Control Kit V2");
    Serial.println("IR Receiver Raw Data + Button Decode Test");
  }
  irrecv.enableIRIn(); // Start the 12
  pinMode(5, OUTPUT); //Motor A and Motor B drive the wheel of the card dealer car
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT); //Motor C is the drive wheel to deal the card
  pinMode(10, OUTPUT);
  pinMode(7, INPUT);
  digitalWrite(5, LOW);
  digitalWrite(3, LOW);
  digitalWrite(6, LOW);
  digitalWrite(9, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  //randomSeed(analogRead(9));  //random number for random card dealing
  //InitCompass();
  compass.begin();
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    if (DEBUGLEVEL) Serial.println(results.value, HEX); // UN Comment to see raw values
    //TranslateIR();
    translateir();
    irrecv.resume(); // receive the next value
  }


int Player(int pos)
{
  pos = pos % 360;
  int prePos = compass.readHeading() - pos;
  if (DEBUGLEVEL) {
    Serial.print("parameter pos is:");
    Serial.println(pos);
    Serial.print("first prePos is:");
    Serial.println(prePos);
  }
  analogWrite(5, SPEED1);
  //digitalWrite(5, HIGH); // start player motor
  if (DEBUGLEVEL) Serial.println("Motor A pin d set to high");
  analogWrite(9,SPEED2);
  //digitalWrite(9, HIGH); // start player motor
  if (DEBUGLEVEL) Serial.println("Motor B pin r set to high");
  unsigned long t = millis();
  while ((millis() - t) < 4000) {
    if (DEBUGLEVEL) Serial.println("Enter while loop of Player");
    delay(5);
    int curPos = compass.readHeading() - pos;
    if (DEBUGLEVEL) {
      Serial.print("prePos is                :");
      Serial.println(prePos);
      Serial.print("curPos is                  :");
      Serial.println(curPos);
    }
    if ((prePos <= 0) && (curPos >= 0)) {
      digitalWrite(5, LOW); // stop player motor
      if (DEBUGLEVEL) Serial.println("Motor A pin d set to low");
      digitalWrite(9, LOW); // stop player motor
      if (DEBUGLEVEL) Serial.println("Motor B pin r set to low");
      if (DEBUGLEVEL) Serial.println("Turn to right angle");
      return 1;
    }
    prePos = curPos;
  }
  if (DEBUGLEVEL) Serial.println("Time exceed 4s");
  digitalWrite(5, LOW); // stop player motor
  if (DEBUGLEVEL) Serial.println("Motor A pin d set to low");
  digitalWrite(9, LOW); // stop player motor
  if (DEBUGLEVEL) Serial.println("Motor B pin r set to low");                                                                                                                                   
  return 0;
}


int PlayCard()
{
  delay(5);
  digitalWrite(10, HIGH);
  if (DEBUGLEVEL) Serial.println("Motor C pin d set to high");
  unsigned long t = millis();
  while ((millis() - t) < iDealCards) {
    if (digitalRead(7) == LOW) {
      if (DEBUGLEVEL) Serial.println("Low signal detected of the infrared obstacle avoidance");
      delay(70);
      digitalWrite(10, LOW);
      if (DEBUGLEVEL) Serial.println("Motor C pin d set to low");
      return 1;
    }
  }
  digitalWrite(10, LOW);
  if (DEBUGLEVEL) {
    Serial.println("time exceeds iDealCards 2s");
    Serial.println("Motor C pin d set to low");
  }
  return 0;
}


void Deal()
{
  int pos = 60;
  for (int i = 0; i < iPlayers * iCardEach; i++) {
    if (Player(pos) == 0) break;
    if (PlayCard() == 0) break;
    pos = (pos + iPlayerDistance) % 360;
    delay(200);
  }
}


/* Press number it has 3 cases:
  1. After VOL-, set player numbers
  2. After VOL+, set cards for each player
  3. Just the number, turn to the player
*/
void NumberProcessing(int number) {
  if (DEBUGLEVEL) Serial.println(number);
  if (isSetPlayer) {
    iPlayers = number;
    isSetPlayer = false;
  }
  else if (isSetCardForEach) {
    if (isDoneSetCardForEach) {
      iCardEach = number;
      isDoneSetCardForEach = false;
    }
    else
    {
      iCardEach = iCardEach * 10 + number;
      isSetCardForEach = false;
    }
  }
  else
    Player((number - 1)*iPlayerDistance + 60);
}



void translateir() // takes action based on IR code received // describing Car MP3 IR codes
{
  switch (results.value)
  {

    case 0x3778AFF2:
      if (DEBUGLEVEL) Serial.println(" OK PLAY/PAUSE ");
      //delay(500);
      Deal();
      //delay(500);
      break;


    case 0xA877CD25:
      if (DEBUGLEVEL) Serial.println(" 1 ");
      NumberProcessing(1);
      break;
    case 0x4FDC3242:
      if (DEBUGLEVEL) Serial.println(" 2 ");
      NumberProcessing(2);
      break;
    case 0x9FF90E1F:
      if (DEBUGLEVEL) Serial.println(" 3 ");
      NumberProcessing(3);
      break;
    case 0xCD39E45D:
      if (DEBUGLEVEL) Serial.println(" 4 ");
      NumberProcessing(4);
      break;

    case 0x20DB57B9:
      if (DEBUGLEVEL) Serial.println(" 5 ");
      NumberProcessing(5);
      break;

    case 0x5D8B2362:
      if (DEBUGLEVEL) Serial.println(" 6 ");
      NumberProcessing(6);
      break;

    case 0xC7CA768D:
      if (DEBUGLEVEL) Serial.println(" 7 ");
      NumberProcessing(7);
      break;

    case 0xD3969CC3:
      if (DEBUGLEVEL) Serial.println(" 8 ");
      NumberProcessing(8);
      break;

    case 0xD6284F44:
      if (DEBUGLEVEL) Serial.println(" 9 ");
      NumberProcessing(9);
      break;

    default:
      if (DEBUGLEVEL) Serial.println(" other button ");
      break;
  }
  delay(500);
} //END translateir

how is the motor connected? via a relay? H-bridge?
sounds as though it is a power supply problem
when the motor switches on it can overload the supply causing a voltage drop and electrical noise
if you replace the motor with an LED does the program work OK?
if so you have a power supply problem. Have seperate power supplies for the arduino and motor?

...in addition to the good suggestions already posted....

Have you tested running the system without IR, by using serial to send the commands instead. If that works then you may be getting inteference from the motor to the IR sensor. If this turns out to be the case, please post the model # of IR sensor (or link) and a photo of how you have wired it all up.

The other common issue that can sometimes occur is a conflict between the libraries if they are using the same libraries.